Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the 'request' event instead of calling the first event handler di… #1107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions HISTORY.md
@@ -1,5 +1,5 @@
unreleased
==========
3.6.7 / 2018-03-06
==================

* deps: finalhandler@1.1.1
- Fix 404 output for bad / missing pathnames
Expand Down
21 changes: 20 additions & 1 deletion index.js
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
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "connect",
"description": "High performance middleware framework",
"version": "3.6.6",
"version": "3.6.7",
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
"contributors": [
"Douglas Christopher Wilson <doug@somethingdoug.com>",
Expand Down
60 changes: 60 additions & 0 deletions test/server.js
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