Skip to content

danielthepope/express-simple-redirect

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

⏩ Express middleware to redirect to external URLs

Resources

License

Stars

Watchers

Forks

Packages

No packages published