Skip to content

Commit

Permalink
refactored res.redirect() with respondTo()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 23, 2012
1 parent e229118 commit a47660b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,15 @@ res.redirect = function(url){
}

// Support text/{plain,html} by default
if (req.accepts('html')) {
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';
this.set('Content-Type', 'text/html');
} else {
body = statusCodes[status] + '. Redirecting to ' + url;
this.set('Content-Type', 'text/plain');
}
this.respondTo({
'text/plain': function(){
body = statusCodes[status] + '. Redirecting to ' + url;
},

'text/html': function(){
body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';
}
})

// Respond
this.statusCode = status;
Expand Down
2 changes: 1 addition & 1 deletion test/res.redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe('res', function(){

request(app)
.get('/')
.set('Accept', 'text/plain')
.set('Accept', 'text/plain, */*')
.end(function(res){
res.headers.should.have.property('location', 'http://google.com');
res.body.should.equal('Moved Temporarily. Redirecting to http://google.com');
Expand Down

0 comments on commit a47660b

Please sign in to comment.