rerouter

0.1.3 • Public • Published

reroute

Express/Connect middleware for mapping equivalent request paths. See the npm and github pages.

Usage

Declare a source -> target simple map

{source: target,
 source: target,
 ...}

to make two or more request.path (or request.route) values equivalent in the remaining middleware chain.

This is done by rewriting node's request object url. This propagates to express's request object path and route because they are getters from a clever library that uses memoization (and, importantly for us, invalidation) to get parsed URL properties.

All other request properties like headers or query parameters, are unaffected.

Examples

Example: use a nonstandard index query page:

var express = require('express'),
    rerouter = require('rerouter');

app.use(rerouter({
    '/':            '/weird.html',
    '/index.html',  '/weird.html',
    '/index.htm',   '/weird.html'
}));

Example: introduce a shortened user profile query route when you have already defined a longer version in your application later on

var express = require('express'),
    rerouter = require('rerouter');

app.use(rerouter({
    '/usr/id':     '/user/active/profile/id'
}), 'dev');

//  ^---^ notice the dev logging mode

Equivalencies

Until options are introduced, all sources and targets are forced into this standard form:

  • a leading slash
  • all lowercase
  • no trailing slash

which means all of these map objects are equivalent, with the first one standard:

{'/':           '/example.html',
 '/usr/id':    '/users/active/profile/id'}

{'':           'example.html',
 'usr/id':    'users/active/profile/id'}

{'/':           '/example.html/',
 '/usr/id/':    '/users/active/profile/id/'}

Compliance with the URI spec (i.e. illegal character presence) is not yet tested.

Roadmap

  • validate source and target strings against the URI path spec

  • allow case-sensitivity, strict trailing / behaviour, etc. options

    • mirror express's config set strings
  • allow regex sources

  • with regex sources, allow match references in the target

Package Sidebar

Install

npm i rerouter

Weekly Downloads

8

Version

0.1.3

License

none

Last publish

Collaborators

  • kdbanman