Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,15 @@ app.use = function use(fn) {
*/

app.route = function route(path) {
return this.router.route(path);
var routeObj = this.router.route(path);
var self = this;

// TODO: This isn't the cleanest, but it's non-blocking and ensures synchronous call chains to the route object (ex: .get()) have ran before we trigger the event.
setTimeout(function() {
self.emit('route', routeObj);
},0);

return routeObj;
};

/**
Expand Down Expand Up @@ -477,6 +485,8 @@ methods.forEach(function (method) {

var route = this.route(path);
route[method].apply(route, slice.call(arguments, 1));
// The emit call works when placed here for direct METHOD call on the app, but not when using .route()
//this.emit('route', route);
return this;
};
});
Expand Down
21 changes: 21 additions & 0 deletions test/app.route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

var assert = require('node:assert')
var express = require('../');
var request = require('supertest');

Expand Down Expand Up @@ -62,6 +63,26 @@ describe('app.route', function(){
.expect(404, done);
});

it('should emit "route" when a new route is defined', function(done){
var app = express()

app.on('route', function(arg){
var layer = (app.stack || app.router.stack).find(function(i){
return i.route.path === '/:foo';
});

assert.strictEqual(arg, layer.route);
assert.strictEqual(arg.path, '/:foo');
assert.strictEqual(arg.methods.get, true);
done();
});

app.route('/:foo')
.get(function(req, res) {
res.send(req.params.foo);
});
});

describe('promise support', function () {
it('should pass rejected promise value', function (done) {
var app = express()
Expand Down
19 changes: 19 additions & 0 deletions test/app.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ describe('app.router', function () {
var app = express();
assert.throws(app[method].bind(app, '/', 3), /argument handler must be a function/);
})

it('should emit "route" when a new route is defined using ' + method.toUpperCase(), function(done){
var app = express()

app.on('route', function(arg){
var layer = (app.stack || app.router.stack).find(function(i){
return i.route.path === '/:foo';
});

assert.strictEqual(arg, layer.route);
assert.strictEqual(arg.path, '/:foo');
assert.strictEqual(arg.methods[method], true);
done();
});

app[method]('/:foo', function(req, res) {
res.send(req.params.foo);
});
});
});

it('should re-route when method is altered', function (done) {
Expand Down