named-routing-system
v1.0.5
Published
Another named routing system for node.js (express.js)
Downloads
4
Readme
Named Routing System
Another named routing system for express.js. This system is inspired by Django's.
Features
- Prevents url hard-coding
- Clean and user friendly URL Mapping
- Works in both middlewares and templates
- Lightweight module without dependencies
Install
npm install named-routing-system
Usage
Configuration
// app.js
var urlMapping = require('named-routing-system');
var routing = require('./url');
urlMapping(routing.url_collection, app);
URL generation
// url.js
var home = require('./routes/home');
var user = require('./routes/user');
exports.url_collection = [
{ pattern: '/', view: home.index, name: 'home.index' },
{ pattern: '/user', view: user.collection, name: 'user.collection' },
{ pattern: '/user/:id', view: user.display, name: 'user.display', methods: ['get'] },
{ pattern: '/user/:id', view: user.update, name: 'user.update', methods: ['post'] },
...
];
Middleware files
//routes/home.js
exports.index = function(req, res) {
res.render('home');
};
Template files
//views/home.jade
a(href="#{url('user')}") User Collection
a(href="#{url('user.display', { id: 'user_id' })}") Display User Profile