weikee94 / blog-api

Node.js 从零开发web server博客项目
0 stars 0 forks source link

10 博客项目 koa2 🥳 #7

Open weikee94 opened 4 years ago

weikee94 commented 4 years ago

Content

weikee94 commented 4 years ago

async await demo

// async await method
readFileData = async () => {
  // 同步写法
  const aData = await getFileContentPromise("a.json");
  console.log("a data: ", aData);
  const bData = await getFileContentPromise(aData.next);
  console.log("b data: ", bData);
  const cData = await getFileContentPromise(bData.next);
  console.log("c data: ", cData);
};

readFileData();
weikee94 commented 4 years ago

koa2 install

weikee94 commented 4 years ago

koa2 router 配置

const router = require("koa-router")();

router.prefix("/api/blog");

router.get("/list", async function (ctx, next) {
  const query = ctx.query;
  ctx.body = {
    errno: 0,
    query,
    data: ["获取博客列表"],
  };
});

module.exports = router;