Removed `app.redirect()`
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 0f72ca8 · Browse files · 3 files·+1 added−100 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+1 added−100 removed
- tests:+0 added−47 removed
- other:+1 added−53 removed
Provenance
- Commit
4329528- Tree
d8092bc- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
0f72ca8
3 files changed+1 added−100 removed
modified:lib/application.js-15 removed
41 unmodified lines42 var self = this;43 this.cache = {};44 this.settings = {};45 this.engines = {};46- this.redirects = {};46 this.isCallbacks = {};47 this.viewCallbacks = [];48 this.defaultConfiguration();49 357 unmodified lines407 return this.set(setting, false);408 };409 410 /**412- * Redirect `key` to `url`.413- *414- * @param {String} key415- * @param {String} url416- * @return {app} for chaining417- * @api public418- */419-420-app.redirect = function(key, url){421- this.redirects[key] = url;422- return this;423-};424-425-/**411 * Configure callback for zero or more envs,412 * when no env is specified that callback will413 * be invoked for all environments. Any combination414 * can be used multiple times, in any order desired.143 unmodified linesmodified:lib/response.js+1 added-38 removed
443 unmodified lines444 * While the leading slash would result in a redirect to "/login":445 *446 * res.redirect('/login');447 *448- * Redirect Mapping:449- * 450- * To extend the redirect mapping capabilities that Express provides,451- * we may use the `app.redirect()` method:452- * 453- * app.redirect('google', 'http://google.com');454- * 455- * Now in a route we may call:456- *457- * res.redirect('google');458- *459- * We may also map dynamic redirects:460- *461- * app.redirect('comments', function(req, res){462- * return '/post/' + req.params.id + '/comments';463- * });464- *465- * So now we may do the following, and the redirect will dynamically adjust to466- * the context of the request. If we called this route with _GET /post/12_ our467- * redirect _Location_ would be _/post/12/comments_.468- *469- * app.get('/post/:id', function(req, res){470- * res.redirect('comments');471- * });472- *473- * Unless an absolute `url` is given, the app's mount-point474- * will be respected. For example if we redirect to `/posts`,475- * and our app is mounted at `/blog` we will redirect to `/blog/posts`.476- *448 * @param {String} url449 * @param {Number} code450 * @api public451 */17 unmodified lines469 back: req.header('Referrer', root)470 , home: root471 };472 502- // support custom redirect map503- map.__proto__ = app.redirects;504-505- // attempt mapped redirect506- var mapped = 'function' == typeof map[url]507- ? map[url](req, this)508- : map[url];509-473 // perform redirect511- url = mapped || url;474+ url = map[url] || url;475 476 // relative477 if (!~url.indexOf('://')) {478 var path = app.path();101 unmodified linesdeleted:test/app.redirect.js-47 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
var express = require('../') , request = require('./support/http');
describe('res', function(){ describe('.redirect(name, url)', function(){ it('should map a redirect url', function(done){ var app = express();
app.redirect('google', 'http://google.com');
app.use(function(req, res){ res.redirect('google'); });
request(app) .get('/') .end(function(res){ res.headers.should.have.property('location', 'http://google.com'); done(); }) }) }) describe('.redirect(name, fn)', function(){ it('should map a redirect to a callback', function(done){ var app = express();
app.redirect('comments', function(req){ return '/post/' + req.post.id + '/comments'; });
app.use(function(req, res){ req.post = { id: 1 }; res.redirect('comments'); });
request(app) .get('/') .set('Host', 'example.com') .end(function(res){ res.headers.should.have.property('location', 'http://example.com/post/1/comments'); done(); }) }) })})