server/index.js
成功代码
const User = require('./models/user');
const router=require('koa-router')();
router.post('/user/login',async (ctx,next) => {
ctx.body = await User.findOne({username:'test'}).exec();
next();
})
const app=new Koa();
app
.use(cors())
.use(bodyparser())
.use(static(path.resolve(__dirname,'build')))
.use(router.routes())
.use(async (ctx,next) => {
console.log(`\n\n\n${ctx.method} ${ctx.url} ${new Date().toLocaleString()}`);
next();
console.log(`-----------------${new Date().toLocaleString()}------------\n`,ctx.body);
});
失败,ctx.body=undefined
app
.use(cors())
.use(bodyparser())
.use(static(path.resolve(__dirname,'build')))
.use(async (ctx,next) => {
console.log(`\n\n\n${ctx.method} ${ctx.url} ${new Date().toLocaleString()}`);
next();
console.log(`-----------------${new Date().toLocaleString()}------------\n`,ctx.body);
})
.use(router.routes());
区别在于中间件routers的位置,but why?