Sails.js 生产环境不会 return JSON 响应 badRequest 但开发环境会
Sails.js Production Environment does not return JSON response on badRequest but dev environment does
如标题所述,我的 sails 应用程序 GET 请求特定 route/controller 函数 returns badRequest 与 JSON 在开发环境中而不是在生产环境中。这是为什么?
控制器函数如下:
index: function(req, res) {
async.auto({
companies: function(cb) {
User.findOneById(req.session.user.id)
.populate('companies')
.exec(function(err, user)
{
if(err) {
var badRequestData = { error: err };
return cb(badRequestData, null);
} else if(user.companies.length == 0) {
var badRequestData = { error: "This user has no associated companies." };
return cb(badRequestData, null);
}
cb(null, user.companies)
});
},
validateForNullCompanies: ['companies', function(cb, results) {
var nullCompanies = _.where(results.companies, { stripeAccountId: null });
if(nullCompanies.length > 0) {
var badRequestData = { error: "This user needs to authenticate stripe with their company." };
return cb(badRequestData, null);
} else {
return cb();
}
}]
}, function(err, results) {
if (err) {
return res.badRequest(err);
}
return res.ok();
});
},
刚好碰到这个,
如果您查看 http://sailsjs.org/documentation/reference/response-res/res-bad-request 中的注释,上面写着
By default, the specified error (err) will be excluded if the app is running in the "production" environment (i.e. process.env.NODE_ENV === 'production').
看看api/responses/badRequest.js
我们可以看到下面的代码:
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if (sails.config.environment === 'production') {
data = undefined;
}
因此,如果您将其注释掉,我相信您会在生产中得到您想要的结果。同时,它出现在这里似乎是有原因的,所以可能会发回带有 res.send([statusCode], body)
的自定义错误代码,其中 body 是您的 JSON,或者创建一些客户端处理,并提供较少的描述性解释错误的请求。
类似于finnergizer提供的答案,如果查看api/responses/badRequest.js,实际代码应该是这样的:
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if (sails.config.environment === 'production' && sails.config.keepResponseErrors !== true) {
data = undefined;
}
原因在评论里已经说了。如果你还想在生产环境中显示错误信息,而不是注释掉这一行,其实你可以在config/env/production.js中添加'keepResponseErrors'项并设置为true。像这样:
module.exports = {
keepResponseErrors: true
};
这样就不用每api/response个js文件都改
我遇到了同样的问题,并通过在 config/env/production.js
中添加以下代码解决了
module.exports = {
keepResponseErrors: true
}
如标题所述,我的 sails 应用程序 GET 请求特定 route/controller 函数 returns badRequest 与 JSON 在开发环境中而不是在生产环境中。这是为什么?
控制器函数如下:
index: function(req, res) {
async.auto({
companies: function(cb) {
User.findOneById(req.session.user.id)
.populate('companies')
.exec(function(err, user)
{
if(err) {
var badRequestData = { error: err };
return cb(badRequestData, null);
} else if(user.companies.length == 0) {
var badRequestData = { error: "This user has no associated companies." };
return cb(badRequestData, null);
}
cb(null, user.companies)
});
},
validateForNullCompanies: ['companies', function(cb, results) {
var nullCompanies = _.where(results.companies, { stripeAccountId: null });
if(nullCompanies.length > 0) {
var badRequestData = { error: "This user needs to authenticate stripe with their company." };
return cb(badRequestData, null);
} else {
return cb();
}
}]
}, function(err, results) {
if (err) {
return res.badRequest(err);
}
return res.ok();
});
},
刚好碰到这个,
如果您查看 http://sailsjs.org/documentation/reference/response-res/res-bad-request 中的注释,上面写着
By default, the specified error (err) will be excluded if the app is running in the "production" environment (i.e. process.env.NODE_ENV === 'production').
看看api/responses/badRequest.js
我们可以看到下面的代码:
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if (sails.config.environment === 'production') {
data = undefined;
}
因此,如果您将其注释掉,我相信您会在生产中得到您想要的结果。同时,它出现在这里似乎是有原因的,所以可能会发回带有 res.send([statusCode], body)
的自定义错误代码,其中 body 是您的 JSON,或者创建一些客户端处理,并提供较少的描述性解释错误的请求。
类似于finnergizer提供的答案,如果查看api/responses/badRequest.js,实际代码应该是这样的:
// Only include errors in response if application environment
// is not set to 'production'. In production, we shouldn't
// send back any identifying information about errors.
if (sails.config.environment === 'production' && sails.config.keepResponseErrors !== true) {
data = undefined;
}
原因在评论里已经说了。如果你还想在生产环境中显示错误信息,而不是注释掉这一行,其实你可以在config/env/production.js中添加'keepResponseErrors'项并设置为true。像这样:
module.exports = {
keepResponseErrors: true
};
这样就不用每api/response个js文件都改
我遇到了同样的问题,并通过在 config/env/production.js
中添加以下代码解决了module.exports = {
keepResponseErrors: true
}