0.2.2 • Published 9 years ago
obj-to-express-router v0.2.2
Object to Express Router
A simple lib to translate javascript objects into Express Routers.
Install
npm install obj-to-express-router --save
How to use with a express example
var express = require('express')
, app = express()
, objToRouter = require('obj-to-express-router')(__dirname+'/routes');;
// mount in route /api/news and require file from
// __dirname + 'routes/api/news.js'
app.use( '/api/news', objToRouter('api/news', express.Router()) );
app.listen(3000);
Javascript file object example
exports.show = { //action object
path: '/:id', // route path
verb: 'get', // http verb
mids: [ // optional array of middlewares (functions)
function logSomething(req, res, next) {
console.log('show news with id:' + req.params.id);
next();
}
],
fn: function(req, res) { // main function
res.json({
title: 'Google buy apple.',
url: 'google-buy-apple',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
});
}
}
Test example in browser
http://localhost:3000/api/news/123