Friday, 12 May 2017

nodejs - Weird "Can't set headers after they are sent" case

I'm having a strange Can't set headers after they are sent crash on my NodeJs/Express app.

The crashing request : POST /auth/form

How the request is server-side handled

app.js

[...]
var auth_route = require('./routes/auth');
app.use('/auth', auth_route );
[...]

auth.js

var AuthController = require('../controller/auth_controller');

[...]

/**
 * Form AUTH : Login/Password
 */
router.post("/form", function(req, res) {
    var auth_ctrl = new AuthController(req, res);

    if (typeof req.body.username === "undefined" ||
        req.body.username == "" ||
        typeof req.body.password === "undefined" ||
        req.body.password == "")
            return (Response.send_error_response(res, 400, "Missing username or password"));
    auth_ctrl.form_login(req.body.username, req.body.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"))
        bcrypt.compare(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 source code if needed

function send_error_response(res, code, message, missing_fields) {
    var code = code || "";
    var message = message || "";
    var missing_fields = missing_fields || "";

    res.header('Content-Type', 'application/json')
    res.status(code).send(JSON.stringify({
        code: code,
        message: message,
        missingfields: missing_fields
    }));
}

The callstack trace

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

How I make this crash happen

I keep pulling down my enter button which send a looot of request from the same origin.

It seems that's happening inside bcrypt callback function...

Any suggestions ? Thanks in advance !



via Maincore

No comments:

Post a Comment