node koa最简单的实现

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.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"
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_41069726/article/details/88757482