Express middleware to redirect to external URLs
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
test
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json

README.md

express-simple-redirect

Build Status Dependency Status

This module takes express routes and redirects them to external or internal URLs.

It is very similar to redirects on NPM. In fact, I based my tests on the tests in redirects. The difference between the two is my one doesn't rely on any other external libraries, so doesn't have as many features, hence the name 'simple'.

npm install express-simple-redirect --save

Basic usage

var redirect = require('express-simple-redirect');

// a bunch of express route definitions

app.use(redirect({
  '/my-url': 'http://example.com',  // Redirect to external URL
  '/info': '/about'                 // Internal redirect to /about page
}));

Custom response code

You can also add a redirect HTTP status code. By default, this code is 307 (Temporary Redirect). You may wish to change it to 301 for a permanent redirection by doing:

app.use(redirect({
  // urls
}, 301));

Note: Response code 301 adds an entry to the client browser cache, meaning that the browser won't hit your server again. 301 is not recommended for routes that may change.

Loading from a file

You may want to separate your redirects even further by putting them in a different file. This library doesn't have any special way of parsing files, so you need to convert it into a JavaScript object first. The easiest way to do this is by writing your redirects as a JSON file, then you can load it by using require().

redirects.json

{
  "/url": "http://www.example.com",
  "/info": "/about",
  ...
}

app.js

app.use(redirect(require('./redirects.json')));