2016-12-07 62 views
1

返回之前,我继承了一些非常凌乱环回代码,我想知道是否有可能超过200设置状态码回送

返回的状态码例如,我有以下代码:

PensionsUser.loginView = function (ctx, credentials, cb) { 
    var respJSON = util.getresponseTemplate(); 

    credentials.email = credentials.email.toLowerCase().trim(); 
    PensionsUser.login(credentials, 'user', function (err, loginResp) { 
     if (err) { 
     util.addErrorToResponse(err, respJSON, 'NO-PUS-LV-001'); 
     app.log.error(util.loggingTemplate('PensionsUser', 'loginView', 'NO-PUS-LV-001', credentials.email)); 
     ctx.res.status = 401; //does not work 
     cb(null, respJSON); 
    //etc. 

我知道cb(null, respJSON)应该返回错误这样cb(respJSON)但遗憾的是前端代码依赖于如目前正在返回此JSON是,所以我的第一个步骤将是刚刚更改的状态代码。

这可能吗?

回答

2

您可以设置自定义错误,如{错误:500,消息:“自定义错误“},或者您可以在中间件配置文件中配置”强大的错误处理程序“。

0

要根据自己的要求发送响应代码,您可以使用此代码:

"YourModelName"."YourRemoteMethod" = function("YourAcceptArguments", callback) { 

     var error = new Error("New password and confirmation do not match"); 
     error.status = "Enter the resposne code that you want to send"; 
     // Example => error.status = 400 
     return callback(error); 
    };