A little more messing around gave the answer:
var express = require('express'); var server = express(); var mw1 = function(req, resp, next) { //do stuff if (success) { next(); } else { resp.send(406, 'Invalid because of this'); req.connection.destroy(); //without calling next() } }; var mw2 = function(req, resp, next) { //do stuff if (success) { next(); } else { resp.send(406, 'Invalid because of that'); req.connection.destroy(); //without calling next() } }; server.post('/some/path', [mw1, mw2], function(req, resp) { //write response });
The trick sent a response: resp.send(406, 'Invalid because of this');
Before destroying a connection: req.connection.destroy();
In fact, without destroying the connection, I found that it also works in the general case.
(But this was necessary in my particular case and is beyond the scope of this question.)
If a response has already been sent, then the expression will not automatically call next() for you, as it seems otherwise.
bguiz
source share