Skip to content

Commit

Permalink
Merge pull request #164 from Medium/jamie-permanent-redirect-support
Browse files Browse the repository at this point in the history
Support permanent redirects
  • Loading branch information
majelbstoat committed Mar 18, 2015
2 parents c1f110c + 1ae0a35 commit aea16db
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matador",
"description": "an MVC framework for Node",
"version": "2.0.0-alpha.9",
"version": "2.0.0-alpha.10",
"homepage": "https://github.com/Medium/matador",
"main": "src/matador.js",
"authors": [
Expand Down
6 changes: 6 additions & 0 deletions src/matador.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ function requestDecorator(app, err, req, res, next) {
res.end()
}

// Add a method to permanently redirect the response to a new url.
res.redirectPermanent = function redirectRequestPermanent(url) {
res.writeHead(301, {'Location': url})
res.end()
}

// Add a method for sending output to the response. Defaults to HTML.
res.send = function sendResponse(data, headers, status) {
var bytesWritten
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = function (app) {
return {
'/redirect': 'Home.redirect',
'/redirectPermanent': 'Home.redirectPermanent',
'/target': 'Home.target',
'/queryStrings': 'Home.queryStrings'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ module.exports = function (app, config) {

util.inherits(HomeController, ApplicationController)

HomeController.prototype.redirectPermanent = function (req, res) {
res.redirectPermanent('/elsewhere')
}

HomeController.prototype.redirect = function (req, res) {
res.redirect('/target')
}
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/request_response_decorators_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ var falkor = require('falkor')

var port = process.env.PORT || 3000

exports.testResponsePermanentRedirect = falkor.fetch('http://localhost:' + port + '/redirectPermanent')
.expectStatusCode(301)
.expectHeader('Location', '/elsewhere')

exports.testResponseRedirect = falkor.fetch('http://localhost:' + port + '/redirect')
.expectStatusCode(302)
.expectHeader('Location', '/target')
Expand Down

0 comments on commit aea16db

Please sign in to comment.