index.js
const Koa =require("koa")
const Router =require("koa-router")
const koaBody =require("koa-body")
const appRouter =new Router()
const app =new Koa()
appRouter.post("/hello",(ctx,next)=>{
let bd =ctx.request.body;
ctx.res.setHeader("Access-Control-Allow-Origin", "*");
ctx.body={
code:0,
mst:`你好, ${bd.name} ,请求成功`
}
})
app.use(koaBody())
.use(appRouter.routes())
app.listen(4000,()=>{
console.log("opening ...")
})
package.json
{
"name": "koaDemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"koa": "^2.7.0",
"koa-body": "^4.1.0",
"koa-router": "^7.4.0"
}
}