api 라우트 내부에 posts 라우트를 만들어줄겁니다
api폴더를 만들고, 그 내부에 index.js 파일을 만들어주세요
src/api/posts/index.js
const Router = require("koa-router");
const posts = new Router();
const printInfo = ctx =>{
ctx.body = {
method: ctx.method,
path: ctx.path,
params: ctx.params
}
}
posts.get("/", printInfo);
posts.post("/", printInfo);
posts.get("/:id", printInfo);
posts.delete("/:id", printInfo);
posts.put("/:id", printInfo);
posts.patch("/:id", printInfo);
module.exports = posts
이 객체에는 현재 요청의 메서드, 경로, 파라미터를 담았습니다.
코드를 완성한 후 api 라우트에 posts 라우트를 연결하세요.(전에 한 방법과 비슷)
src/api/index.js
const Router = require("koa-router");
const api = new Router();
const posts = require("./posts")
api.use("/posts", posts.routes());
//router를 내보냅니다
module.exports = api
(기존 test는 지웠습니다.)
나머지 api는 테스트할때 postman을 사용할겁니다.
이런식으로 너무 잘 작동합니다!
ps. 내가 api를 만들다니... 엄청 감격했음... 진짜 울뻔함