20220731_TIL_개인과제 호출구조

codeing999·2022년 7월 31일
0

CRUD BACKEND 구현

목록 보기
3/10

코드 전체가 아니라 호출과 관련된 코드만 가져옴.

import routes from "./routes/routers/index.js";

const app = express();
const port = 4000;


app.use("/", routes);

가장 먼저 실행되는 app.js에서 ./routes/routers/index.js 를 import하고
app.use("/", routes); 를 통해 기본 path를 통한 request들을 그쪽에서 다루게 한다.

import noteRouter from "./note.js";
import commentRouter from "./comment.js";

const router = express.Router();

router.use('/note', noteRouter);
router.use('/comment', commentRouter);

export default router;

index.js에서는 ./note.js" 등을 import하고
router.use('/note', noteRouter); 를 통해서 /note path를 통한 request를 그쪽에서 다루게 한다.
지금은 note와 comment만 적어두었지만 로그인, 회원가입 등 여러개가 생길 예정.
이렇게 만들어둔 것 export default router;로 export 했기에 위의 app.js에서 import해서 쓸 수 있었던 것.

import * as noteController from "../controllers/note.js";

const noteRouter = Router();

noteRouter.route('')
    .get(noteController.getNote)
    .post(noteController.postNote);

export default noteRouter;

note.js에서는 "../controllers/note.js"를 import하고
noteRouter.route('')
.get(noteController.getNote)
.post(noteController.postNote);
를 통해서 /note path를 통한 request들 중에서도 method가 get이면 controller 폴더에서 만둘어둔 getNote함수가 다루게 하고 method가 post이면 postNote에서 다루게 한다.
지금은 이거만 적어두었지만 밑에
noteRouter.route('/:noteId').get~~ 등을 더 써서 /note path안에서 다른 추가 경로들도 제어하게 할 것이다.


const postNote = async (req, res, next) => {
...
};

...

export {

    postNote,
    getNote

}

controllers 폴더 내의 note.js는 최종적으로 해당 request에 대해서 어떤 응답을 해줄 것인지 내용을 작성한다.

이 안에서 부를 쿼리문도 나중에 따로 databse 폴더를 만들어서 거기에 작성하고 그걸 여기서 호출할 것이다.

profile
코딩 공부 ing..

0개의 댓글