express-simple-redirect

Express middleware to redirect to other URLs


Keywords
express, middleware, redirect, http, https, url
License
MIT
Install
npm install express-simple-redirect@1.0.1

Documentation

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')));