Skip to content
Myr

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

3 files+100 added−2 removed

modified:lib/router/index.js+12 added-2 removed
129 unmodified lines
130131 // middleware and routes132 var stack = self.stack;133134 // 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 lines
169 req.url = req.url.substr(1);170 slashAdded = false;171 }172173 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 lines
230 removed = layerPath;231 req.url = protohost + req.url.substr(protohost.length + removed.length);232233 // Ensure leading slash231 if (!fqdn && '/' != req.url[0]) {234 if (!fqdn && req.url[0] !== '/') {235 req.url = '/' + req.url;236 slashAdded = true;237 }238239 // 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 }245246 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 lines
added:test/req.baseUrl.js+87 added
12var express = require('..')3var request = require('supertest')45describe('req', function(){6 describe('.baseUrl', function(){7 it('should be empty for top-level route', function(done){8 var app = express()910 app.get('/:a', function(req, res){11 res.end(req.baseUrl)12 })1314 request(app)15 .get('/foo')16 .expect(200, '', done)17 })1819 it('should contain lower path', function(done){20 var app = express()21 var sub = express.Router()2223 sub.get('/:b', function(req, res){24 res.end(req.baseUrl)25 })26 app.use('/:a', sub)2728 request(app)29 .get('/foo/bar')30 .expect(200, '/foo', done);31 })3233 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()3839 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)4546 request(app)47 .get('/foo/bar/baz/zed')48 .expect(200, '/foo/bar/baz', done);49 })5051 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()5758 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 })8182 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
1unreleased2==========34 * 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