Same old setup package

npm install koa-router
Copy the code

Details: www.npmjs.com/package/koa…

But the NPM address does not use the tutorial, the tutorial recommends the address: github.com/ZijianHe/ko…

Waste don’t say much, go straight to the code

const Koa = require("koa"); // import Router module const Router = require("koa-router"); const app = new Koa(); // Create router object const router = new router (); // Process route /* - Receive two arguments + the first argument: route address + the second argument: Receive a callback (CTX and next) - koA responds to the data via the body property of the context CTX - also returns JSON */ router.get("/ API ", (CTX, Next) => {ctx.body = "get successful response string "; }); router.get("/list", (ctx, next) => { ctx.body = { name: "sandy", age: 21, method: "get", }; }); Router. Post ("/registered", (CTX, next) => {ctx.body = "Post successful response string "; }); router.post("/login", (ctx, next) => { ctx.body = { name: "gt", age: 19, method: "post", }; }); Router.routes () starts the routing function. Router.allowedmethods () automatically sets the response header */ app.use(router.routes()).use(router.allowedMethods()); app.listen(999);Copy the code

rendering