问题
I want to send the response
with an error
in my koa app when there is no sessionId
.I explored But didn't get anything helpful for me to do the same. I also used return ctx.throw(401);
for unauthorized
but it is not good, ctx.throw(401);
just sending the "unauthorized", I want to add some specific information and after adding just send the response to the client.
can anyone suggest me what to do the same?
My code is:
index.validateKey = async (ctx, next) => {
await new Promise((resolve, reject) => {
var authorized = ctx.headers.sessionid ? true : false;
if (!authorized) {
return ctx.throw(401); //HERE , I want to send .
}
resolve();
});
await next();
}
回答1:
You can check sessionId in sync so no async await function is needed. Your middleware function can be like this.
async(ctx, next) => {
var authorized = ctx.headers.sessionid ? true : false;
if (!authorized) {
return ctx.throw('session Id required', 401);;
/*HERE , You can send custom message and return.*/
}
/* call next middleware else*/
await next();
})
来源:https://stackoverflow.com/questions/43085204/what-is-the-replacement-of-expresss-res-send-user-xyz-or-res-end-in