requestHandler 需要在最后面 test: mock user controller

2.0/wai-server
Lei OT 10 months ago
parent 70e2b7e32e
commit 8a17767a28

@ -36,8 +36,7 @@ exports.getOne = async ctx => {
}; };
exports.getAll = async ctx => { exports.getAll = async ctx => {
ctx.status = 200; return db.users;
ctx.body = db.users;
}; };
exports.createOne = async ctx => { exports.createOne = async ctx => {
@ -51,6 +50,5 @@ exports.createOne = async ctx => {
}; };
db.users.push(newUser); db.users.push(newUser);
const createdUser = db.users.find(user => user.id === id); const createdUser = db.users.find(user => user.id === id);
ctx.status = 201; return createdUser;
ctx.body = createdUser;
}; };

@ -24,17 +24,18 @@ server
.use(bodyParser) .use(bodyParser)
.use(helmet) .use(helmet)
.use(compress) .use(compress)
.use(cors) .use(cors);
.use(requestHandler);
// Logger // Logger
server.use(async (ctx, next) => { server.use(async (ctx, next) => {
const start = new Date(); const start = new Date();
await next(); await next();
const ms = new Date() - start; const ms = new Date() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`); console.log(`${ctx.method} ${ctx.url} - ${ctx.status} - ${ms}ms`);
}); });
server.use(requestHandler); // 必须在最后
/** /**
* Apply to our server the api router * Apply to our server the api router
*/ */

Loading…
Cancel
Save