add req.baseUrl to access stripped path in routes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2078
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 4c0f1f5 · Browse files · 3 files·+100 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+100 added−2 removed
- tests:+87 added−0 removed
- other:+13 added−2 removed
Provenance
- Commit
739586f- Tree
8aa7d47- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
4c0f1f5
3 files changed+100 added−2 removed
modified:lib/router/index.js+12 added-2 removed
129 unmodified lines130 131 // middleware and routes132 var stack = self.stack;133 134- // request-level next134+ // manage inter-router variables135 var parent = req.next;136+ var parentUrl = req.baseUrl || '';137 done = wrap(done, function(old, err) {138+ req.baseUrl = parentUrl;139 req.next = parent;140 old(err);141 });142 req.next = next;26 unmodified lines169 req.url = req.url.substr(1);170 slashAdded = false;171 }172 173+ req.baseUrl = parentUrl;174 req.url = protohost + removed + req.url.substr(protohost.length);175 req.originalUrl = req.originalUrl || req.url;176 removed = '';177 52 unmodified lines230 removed = layerPath;231 req.url = protohost + req.url.substr(protohost.length + removed.length);232 233 // Ensure leading slash231- if (!fqdn && '/' != req.url[0]) {234+ if (!fqdn && req.url[0] !== '/') {235 req.url = '/' + req.url;236 slashAdded = true;237 }238 239+ // Setup base URL (no trailing slash)240+ if (removed.length && removed.substr(-1) === '/') {241+ req.baseUrl = parentUrl + removed.substring(0, removed.length - 1);242+ } else {243+ req.baseUrl = parentUrl + removed;244+ }245+246 debug('%s %s : %s', layer.handle.name || 'anonymous', layerPath, req.originalUrl);247 var arity = layer.handle.length;248 if (err) {249 if (arity === 4) {174 unmodified linesadded:test/req.baseUrl.js+87 added
1+2+var express = require('..')3+var request = require('supertest')4+5+describe('req', function(){6+ describe('.baseUrl', function(){7+ it('should be empty for top-level route', function(done){8+ var app = express()9+10+ app.get('/:a', function(req, res){11+ res.end(req.baseUrl)12+ })13+14+ request(app)15+ .get('/foo')16+ .expect(200, '', done)17+ })18+19+ it('should contain lower path', function(done){20+ var app = express()21+ var sub = express.Router()22+23+ sub.get('/:b', function(req, res){24+ res.end(req.baseUrl)25+ })26+ app.use('/:a', sub)27+28+ request(app)29+ .get('/foo/bar')30+ .expect(200, '/foo', done);31+ })32+33+ it('should contain full lower path', function(done){34+ var app = express()35+ var sub1 = express.Router()36+ var sub2 = express.Router()37+ var sub3 = express.Router()38+39+ sub3.get('/:d', function(req, res){40+ res.end(req.baseUrl)41+ })42+ sub2.use('/:c', sub3)43+ sub1.use('/:b', sub2)44+ app.use('/:a', sub1)45+46+ request(app)47+ .get('/foo/bar/baz/zed')48+ .expect(200, '/foo/bar/baz', done);49+ })50+51+ it('should travel through routers correctly', function(done){52+ var urls = []53+ var app = express()54+ var sub1 = express.Router()55+ var sub2 = express.Router()56+ var sub3 = express.Router()57+58+ sub3.get('/:d', function(req, res, next){59+ urls.push('0@' + req.baseUrl)60+ next()61+ })62+ sub2.use('/:c', sub3)63+ sub1.use('/', function(req, res, next){64+ urls.push('1@' + req.baseUrl)65+ next()66+ })67+ sub1.use('/bar', sub2)68+ sub1.use('/bar', function(req, res, next){69+ urls.push('2@' + req.baseUrl)70+ next()71+ })72+ app.use(function(req, res, next){73+ urls.push('3@' + req.baseUrl)74+ next()75+ })76+ app.use('/:a', sub1)77+ app.use(function(req, res, next){78+ urls.push('4@' + req.baseUrl)79+ res.end(urls.join(','))80+ })81+82+ request(app)83+ .get('/foo/bar/baz/zed')84+ .expect(200, '3@,1@/foo,0@/foo/bar/baz,2@/foo/bar,4@', done);85+ })86+ })87+})modified:History.md+1 added
1 unreleased2 ==========3 4+ * add `req.baseUrl` to access the path stripped from `req.url` in routes5 * fix behavior of multiple `app.VERB` for the same path6 * fix issue routing requests among sub routers7 * invoke `router.param()` only when necessary instead of every match8 * proper proxy trust with `app.set('trust proxy', trust)`1445 unmodified lines