Skip to content

Instantly share code, notes, and snippets.

@weaver
Created March 14, 2011 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weaver/869889 to your computer and use it in GitHub Desktop.
Save weaver/869889 to your computer and use it in GitHub Desktop.
When nested middleware is used, the `res.app` and `req.app` properties aren't restored when outerNext is called.
// In Express 1.0.8, the `res.app` and `req.app` properties aren't restored when
// outerNext() is called by nested server middleware.
//
// Run this script, then try to visit something handled by the `notFound()` middleware
// (e.g. `http://localhost:3000/mumble`. An assertion error is raised because `res.app`
// is equal to the `middleware()` instead of `app`.
var Assert = require('assert'),
Express = require('express'),
app = Express.createServer();
app.configure(function() {
app
.use(middleware())
.use(app.router)
.use(notFound());
});
function middleware() {
var mid = Express.createServer();
mid.get('/mid/example', function(req, res) {
res.send('middleware here');
});
return mid;
}
function notFound() {
return function(req, res, next) {
Assert.ok(res.app === app, 'Expected main app.');
res.send('not found', 404);
};
}
app.get('/', function(req, res) {
res.send('hello');
});
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment