我的 NodeJs / Express 应用程序发生了奇怪的Can't set headers after they are sent
崩溃。
崩溃请求:POST /auth/form
app.js
[...]
var auth_route = require('./routes/auth');
app.use('/auth', auth_route );
[...]
auth.js
var AuthController = require('../controller/auth_controller');
[...]
router.post("/form", function(req, res) {
[...]
auth_ctrl.form_login(username, password);
});
auth_controller.js
AuthController.prototype.form_login = function(username, password) {
_this.user_model.get_by_username(username, function(user) {
if (user == null)
return (Response.send_204(_this.res, "User not found"))
password.isTheSame(password, user.password, function(err, res) {
if (err)
return (Response.send_error_response(_this.res, 500, "Internal server error occured, sorry about that.."));
if (!res)
return (Response.send_error_response(_this.res, 401, "Wrong password"));
// Crash seems to happen on the above 401 response which is the 67th lines of my auth_controller file (cf. callstack bellow)
_this.start_user_session(user, function(err) {
if (err)
return (Response.send_error_response(_this.res, 500, "Internal server error"));
return (Response.send_200(_this.res, "You are logged!"));
})
});
})
}
如果需要,可以使用Response.send_error_response源代码
function send_error_response(res, code, message, missing_fields) {
[..]
res.header('Content-Type', 'application/json')
res.status(code).send(JSON.stringify({
[..]
}));
}
POST /auth/form 401 2.408 ms - -
_http_outgoing.js:356
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (C:\Users\ME\dev\app\node_modules\express\lib\response.js:719:10)
at Object.send_error_response (C:\Users\ME\dev\app\core\lib\Response.Library.js:30:6)
at C:\Users\ME\dev\app\controller\auth_controller.js:67:22
我一直按下我的输入按钮,该按钮发送来自同一来源的请求。
似乎在密码回调函数中发生了......
有什么建议吗?提前谢谢!
答案 0 :(得分:2)
我的猜测是你AuthController
的实现是这样的:
var _this;
function AuthController(req, res) {
this.req = req;
this.res = res;
_this = this;
}
这会将_this
“提升”到(模块范围的)全局,这会覆盖每个发布到/auth/form
的请求。如果其中两个请求是快速连续发送的,那么最终可能会出现多次使用相同_this
发送响应的情况,这会导致您收到错误:
_this
指向其控制器实例_this
被覆盖以指向其控制器实例_this
发送回应_this
发回响应,从而导致错误所以,不要使用全局变量,而是使用this
:
AuthController.prototype.form_login = function(username, password) {
this.user_model.get_by_username(username, function(user) {
...
});
};
如果愿意,您始终可以创建一个函数范围的变量来保存对它的引用:
AuthController.prototype.form_login = function(username, password) {
var _this = this;
_this.user_model.get_by_username(username, function(user) {
...
});
};