Per the doc, res.write()
expects a String or Buffer, not a Number.
And, it looks like this is the spot in the http.write()
code where it throws an exception if it's not a String or a Buffer.
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new ERR_INVALID_ARG_TYPE('first argument',
['string', 'Buffer'], chunk);
}
It is not clear to me why that error alone would cause the specific error you reported, but it definitely needs to be fixed in order to work properly.
Perhaps the router is catching the exception and trying to set a 500 status? I don't know about that one. Anyway, glad this fixed the problem.
In looking at the Express code more, it does look like there's a try/catch
around a request handler here:
Layer.prototype.handle_request = function handle(req, res, next) {
var fn = this.handle;
if (fn.length > 3) {
// not a standard request handler
return next();
}
try {
fn(req, res, next);
} catch (err) {
next(err);
}
};
This would attempt to turn the above exception into an error response, but if headers were already flushed which they probably would have been when you called res.write()
, then this would cause the error you experienced about headers already sent.