Wondering what’s next for npm?Check out our public roadmap! »

    redirective

    1.1.0 • Public • Published

    redirective

    Shortcut to provide redirect callback to Express routes.

    app.use('/users/:userId', redirect('https://new-server/users/:userId'));

    NPM version Build Status Coverage Status

    Installation

    $ npm install redirective --save

    Usage

    var redirect = require('redirective');
     
    var express = require('express');
    var app = express();
     
    app.use('/my-route', redirect('http://myothersite.com/my-route'));  // default redirect
    app.use('/my-other-route', redirect(301, 'http://myothersite.com/my-other-route')); // permanent redirect
     
     
    app.listen(3000);

    Examples

    Route specific

    app.use('/all-methods', redirect('/my-other-route'));  // All methods

    HTTP Method specific

    app.get('/my-get-route', redirect('/my-other-get-route'));  // GET only

    Fully-qualified URL for redirecting to a different site

    app.get('/my-route', redirect('http://expressjs.com/en/guide/routing.html'));

    Relative to the current URL

    app.get('/blog/admin/', redirect('post/new'));  // Redirects to /blog/admin/post/new
    app.get('/blog/admin', redirect('post/new'));  // Redirects to /blog/post/new

    For additional use cases refer to Express Redirect.

    URL with parameters

    app.get('/users/:userId', redirect('http://my-other-site/app/readers/:userId')); // /users/1234 => http://my-other-site/app/readers/1234
    app.get('/users/:userId/books/:bookId', redirect('/books/:bookId/users/:userId')); // /users/1234/books/6789 => /books/6789/users/1234

    API

    redirect([status,] url);

    Same signature as res.redirect

    • status - A positive integer that corresponds to an HTTP status code.

      Defaults to 302.

    • url - partial or full url to redirect to.

      The url can have Express Route style named parameters.

      Named parameters in redirect url will be substituted with values from same named parameters in source path.

      Refer example.

    License

    MIT

    Install

    npm i redirective

    DownloadsWeekly Downloads

    3

    Version

    1.1.0

    License

    MIT

    Last publish

    Collaborators

    • avatar