ReverseRoute module for Express
$ npm install reverse-route
var express = require('express'),
reverseRoute = require('reverse-route'),
app = express();
// use reverse-route - basic
reverseRoute(app);
app._route('user', '/user/:id').get(function(req, res, next) {
// ... get user
});
<a href="{{ _url('user', { id: 'me' }) }}">My profile</a>
reverseRoute(app, function(params, req) {
params.locale = i18n.getLocale(req);
return params;
});
app._route('signin', '/signin').get(function(req, res, next) {
// render signin page
}).post(function(req, res, next) {
// handle signin request
});
Support GET, POST, PUT, DELETE, HEAD, TRACE, OPTIONS, CONNECT, and PATCH.
app._get('home', '/', function(req, res, next) {
// ...
});
Support alias with defined parameter sets
app._route.add('static', {
about: {
id: 'about'
},
term: {
id: 'term'
}
});
app._route('static', '/page/:id').get(function(req, res, next) {
// render static page
});
<a href="{{ _url('static', 'about') }}">About us</a><br>
<a href="{{ _url('static', 'term') }}">Terms & Conditions</a>
Remove a defined parameter sets
app._route.remove('static', 'term');
function redirectToHomePage(req, res, next) {
// res._redirect(alias)
res._redirect('home');
}
function redirectToProfilePage(req, res, next) {
// res._redirect(alias, params)
res._redirect('user', {
id: 'me'
});
}
function redirectToAboutPage(req, res, next) {
// res._redirect(alias, setname)
res._redirect('static', 'about');
}
function redirectToAboutPageInAnotherLanguage(req, res, next) {
// res._redirect(alias, setname, params)
res._redirect('static', 'about', {
lang: 'vi'
});
}
Accept same arguments as res._redirect()
. Use to generate URL in HTML template
<a href="{{ _url('home') }}">Home page</a> <!-- URL: / -->
<a href="{{ _url('user', { id: 'me' }) }}">My profile</a> <!-- URL: /user/me -->
<a href="{{ _url('static', 'about') }}">About us</a> <!-- URL: /page/about -->
<a href="{{ _url('static', 'about', { lang: 'vi' }) }}">About us in Vietnamese</a> <!-- URL: /page/about?lang=vi -->
$ npm install
$ npm test