Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Delete back as a magic string
  • Loading branch information
blakeembrey committed Sep 10, 2024
commit 7022c489d0f9bd20c1335e253442d8f53e33bf8e
2 changes: 1 addition & 1 deletion examples/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ app.post('/login', function (req, res, next) {
req.session.success = 'Authenticated as ' + user.name
+ ' click to <a href="/logout">logout</a>. '
+ ' You may now access <a href="/restricted">/restricted</a>.';
res.redirect('back');
res.redirect(req.get('Referrer') || '/');
});
} else {
req.session.error = 'Authentication failed, please check your '
Expand Down
4 changes: 2 additions & 2 deletions examples/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.get('/', function(req, res){

app.get('/forget', function(req, res){
res.clearCookie('remember');
res.redirect('back');
res.redirect(req.get('Referrer') || '/');
});

app.post('/', function(req, res){
Expand All @@ -43,7 +43,7 @@ app.post('/', function(req, res){
res.cookie('remember', 1, { maxAge: minute })
}

res.redirect('back');
res.redirect(req.get('Referrer') || '/');
});

/* istanbul ignore next */
Expand Down
2 changes: 1 addition & 1 deletion examples/route-separation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ exports.update = function(req, res){
var user = req.body.user;
req.user.name = user.name;
req.user.email = user.email;
res.redirect('back');
res.redirect(req.get('Referrer') || '/');
};
15 changes: 1 addition & 14 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,26 +785,13 @@ res.cookie = function (name, value, options) {
*/

res.location = function location(url) {
var loc;

// "back" is an alias for the referrer
if (url === 'back') {
loc = this.req.get('Referrer') || '/';
} else {
loc = String(url);
}

return this.set('Location', encodeUrl(loc));
return this.set('Location', encodeUrl(url));
};

/**
* Redirect to the given `url` with optional response `status`
* defaulting to 302.
*
* The resulting `url` is determined by `res.location()`, so
* it will play nicely with mounted apps, relative paths,
* `"back"` etc.
*
* Examples:
*
* res.redirect('/foo/bar');
Expand Down
58 changes: 0 additions & 58 deletions test/res.location.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,6 @@ describe('res', function(){
.expect(200, done)
})

describe('when url is "back"', function () {
it('should set location from "Referer" header', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.set('Referer', '/some/page.html')
.expect('Location', '/some/page.html')
.expect(200, done)
})

it('should set location from "Referrer" header', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.set('Referrer', '/some/page.html')
.expect('Location', '/some/page.html')
.expect(200, done)
})

it('should prefer "Referrer" header', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.set('Referer', '/some/page1.html')
.set('Referrer', '/some/page2.html')
.expect('Location', '/some/page2.html')
.expect(200, done)
})

it('should set the header to "/" without referrer', function (done) {
var app = express()

app.use(function (req, res) {
res.location('back').end()
})

request(app)
.get('/')
.expect('Location', '/')
.expect(200, done)
})
})

it('should encode data uri1', function (done) {
var app = express()
app.use(function (req, res) {
Expand Down