Skip to content

Commit

Permalink
invoke all listeners instead of just the first
Browse files Browse the repository at this point in the history
  • Loading branch information
pixcai committed Jun 15, 2018
1 parent 8e87e27 commit faeee93
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ proto.use = function use(route, fn) {
// wrap sub-apps
if (typeof handle.handle === 'function') {
var server = handle;

server.route = path;
handle = function (req, res, next) {
server.handle(req, res, next);
Expand All @@ -94,7 +95,25 @@ proto.use = function use(route, fn) {

// wrap vanilla http.Servers
if (handle instanceof http.Server) {
handle = handle.listeners('request')[0];
var server = handle;

handle = function (req, res, next) {
var listeners = server.listeners('request');
var error;

try {
for (var i = 0; i < listeners.length; i++) {
if (!res.finished) {
listeners[i].call(server, req, res);
}
}
} catch (e) {
error = e;
}
if (!res.finished) {
next(error);
}
};
}

// strip trailing slash
Expand Down
60 changes: 60 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ describe('app', function(){
.expect(200, 'hello, world!', done);
})

it('should invoke all request listeners', function (done) {
var app = connect();
var router = http.createServer();

router.on('request', function (req, res) {
res.write('hello, ');
});
router.on('request', function (req, res) {
res.end('world!');
});
app.use(router);

var server = http.createServer(app);

request(server)
.get('/')
.expect(200, 'hello, world!', done);
})

it('should be a callable function', function(done){
var app = connect();

Expand Down Expand Up @@ -92,6 +111,25 @@ describe('app', function(){
.expect(200, 'oh, boom!', done);
})

it('should invoke callback on request listener error', function (done) {
var app = connect();
var router = http.createServer();

router.on('request', function (req, res) {
throw new Error('boom!');
});
app.use(router);
app.use(function (err, req, res, next) {
res.end('oh, ' + err.message);
});

var server = http.createServer(app);

request(server)
.get('/')
.expect(200, 'oh, boom!', done);
})

it('should work as middleware', function(done){
// custom server handler array
var handlers = [connect(), function(req, res, next){
Expand Down Expand Up @@ -214,6 +252,28 @@ describe('app', function(){
.expect(200, done);
})

it('should not fire after headers sent in request listener', function (done) {
var app = connect();
var router = http.createServer();

router.on('request', function (req, res) {
res.end('hello');
});
router.on('request', function (req, res) {
res.write('world');
});
app.use(router);
app.use(function (req, res) {
res.end('body');
});

var server = http.createServer(app);

request(app)
.get('/')
.expect(200, 'hello', done);
})

it('shoud have no body for HEAD', function(done){
var app = connect();

Expand Down

0 comments on commit faeee93

Please sign in to comment.