Added dynamic redirect mapping
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent b75246e · Browse files · 2 files·+42 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−8 removed
- tests:+13 added−0 removed
- other:+29 added−8 removed
Provenance
- Commit
0bb4815- Tree
3a04bcc- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
b75246e
2 files changed+42 added−8 removed
modified:lib/express/response.js+29 added-8 removed
140 unmodified linesExpand all1411421431441451451461471481491501471481491501511521531541551561571581591601611621631641651661671681691701711721739 unmodified lines183184185186187188189190191192170171172193194195140 unmodified linesExpand all * "home" setting. Express also supports "home" out of the box, * which can be set via `app.set('home', '/blog');`, and defaults * to '/'. * * Extended Redirect Map: * Redirect Mapping: * * app.redirect = { * google: 'http://google.com', * blog: '/some/blog' * }; * To extend the redirect mapping capabilities that Express provides, * we may use the `app.redirect()` method: * * app.redirect('google', 'http://google.com'); * * Now in a route we may call: * * res.redirect('google'); * * We may also map dynamic redirects: * * app.redirect('comments', function(req, res, params){ * return '/post/' + params.id + '/comments'; * }); * * So now we may do the following, and the redirect will dynamically adjust to * the context of the request. If we called this route with _GET /post/12_ our * redirect _Location_ would be _/post/12/comments_. * * app.get('/post/:id', function(req, res){ * res.redirect('comments'); * }); * * @param {String} url * @param {Number} code * @api public */9 unmodified lines
// Support custom redirect map map.__proto__ = this.app.redirects;
// Attempt mapped redirect var mapped = typeof map[url] === 'function' ? map[url](this.req, this, this.req.params.path) : map[url];
// Perform redirect this.writeHead(status || 302, { 'Location': map[url] || url }); this.writeHead(status || 302, { 'Location': mapped || url }); this.end();};modified:test/response.test.js+13 added
98 unmodified lines99 app2.set('home', '/blog');100 101 app2.redirect('google', 'http://google.com');102 103+ app2.redirect('blog', function(req, res, params){104+ return params.id105+ ? '/user/' + params.id + '/blog'106+ : null;107+ });108+ 109 app.get('/', function(req, res){110 res.redirect('http://google.com', 301);111 });112 19 unmodified lines132 133 app2.get('/google', function(req, res){134 res.redirect('google');135 });136+137+ app2.get('/user/:id', function(req, res){138+ res.redirect('blog');139+ });140 141 assert.response(app,142 { url: '/' },143 { body: '', status: 301, headers: { Location: 'http://google.com' }});21 unmodified lines165 { body: '', status: 302, headers: { Location: '/blog' }});166 assert.response(app2,167 { url: '/google' },168 { body: '', headers: { Location: 'http://google.com' }});169+ assert.response(app2,170+ { url: '/user/12' },171+ { body: '', headers: { Location: '/user/12/blog' }});172 173 }174 };