Giter Site home page Giter Site logo

express-redirect's Introduction

express-redirect Build Status NPM Version

express-redirect offers you simple and blazing fast redirection rules. Even very complex redirect rules don't take longer than 1 ms. It just comes down to concatenating strings.

Say goodbye to writing 10 lines of code for a plain redirection over and over again.

If you want redirections with fancy JS expressions, you should probably check out deviate.

Deprecation Notice

This package isn't actively maintained anymore. It was written for express v3.x, but still seems to work with express v4.x. Use it at your own risk.

If you want to continue this project, ping me and I'll add you as an owner on npm.

Installation

$ npm install express-redirect
var express = require("express")
  , redirect = require("express-redirect");

var app = express();
redirect(app); // mount the plugin

Example

// just a smple redirect
app.redirect("/p/:id", "/page/:id");

// you want it permanent?
app.redirect("/p/:id", "/page/:id", 301);

// if you want to append the query string (?foo=bar)
app.redirect("/p/:id", "/page/:id", 301, true);

// no id, goto page # 1
app.redirect("/p/:id?", "/page/:id(1)");

// only redirect POST (for whatever reason)
app.redirect("/contact", "/thanks", "post")

// a bit more complex
app.redirect(
  "/entry/:entry/comments/:action(view|edit|delete)?/:id([0-9]+)/:reply([0-9]+)?",
  "/entry/:entry/comments/:action(view)/:id/:reply?"
);

// external redirects work too
app.redirect(
  "/google/:query?",
  "https://www.google.de/?q=:query(Nyan+Cat)"
);

API

express-redirect mounts the new method app.redirect(route, target, [status], [method], [qsa]) to your app. You can access it just like app.get() or app.post() etc.

route

The parameter route is a string and is required. It can contain parameters like :id, :year([0-9]{4})? or :action(view|edit). It's basically just the same as a route you would pass to app.get().

target

The parameter target is a string and is required. It can contain parameters like :id, :year? or :action(view), where a ? marks an optional parameter and (someString) is a default value.
The parameters get replaced by their respective counterparts in the route.

app.redirect("/a/:id([0-9]+)?", "/b/:id(1)");
app.redirect("/c/:action(view|delete)?", "/d/:action?");
/a           -> /b/1
/a/100       -> /b/100
/c           -> /d
/c/view      -> /d/view

status

The parameter status is an integer and is optional. It is a HTTP (redirection) status code. It defaults to 307 (Temporary Redirect).

method

The parameter method is a string and is optional. It is a VERB as in express' router. It defaults to all.

qsa

The parameter qsa is a boolean and is optional. It defaults to false. If set to true, the query string will be appended. By default, it will be discarded.

License

(The MIT License)

Copyright (c) 20012-2013 Jan Buschtöns <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

express-redirect's People

Contributors

buschtoens avatar cristiandouce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

express-redirect's Issues

ability to use Regex with substitution placeholders in redirection url ?

hi there,

I have a need to do something like this:

app.redirect(/^\/weblog(?:\/(.+?))\/?$/, 'https://myweblog.example.com/$1/', 301);

whilst the input regex works fine, the '$1' regex substitution placeholder is not being replaced in the generated redirection URL ... so I end up redirecting to https://myweblog.example.com/$1

Is there a way of getting this working (whilst keeping the regex) with the current code? if not, could you give a hint as to where in the code I could fix/implement this best? (Im in a bit of tight-squeeze with regard to time right now so any pointers that would help me write a PR would be great!)

PS. I am using Express v4.13.3

Use ert

Just thought you might be interested in ert which essentially does the template portion of what this module does. It does it a little differently and as a result it loses the ability to do defaults via the bracket syntax, but gains the ability to do more powerful things like addition. Feel free to take it or leave it 😄

Pass the query params with the url

How can i pass the query params of the src url?

from /tests/catgeory/slug?param=34&param2
to /skill-center/tests/catgeory/slug?param=34&param2

app.redirect("/tests/:category/:slug", "/skill-center/tests/:category/:slug", 301);

Certain redirects do not work

I'm trying to setup a number of redirects including some that are several subdirectories deep. One particular case seems to be breaking.

Route "/a/b/c" is set to redirect to "/a/d/e" but instead redirects to "/a/b/a/d/e". Interestingly, a route of the form "/a/b/c" will successfully redirect to "a/d/c" when called for. I will dig into the library today to try to get to the bottom of this if I have the time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.