If Express will be your web framework, look at the express-resource (Github) API routing middleware. You define resources and include a REST style for you with a very small template.
app.resource('horses', require('./routes/horses'), { format: json })
Given the above, express-resource will connect all REST-style routes to your actions, returning JSON by default. In routes/horses.js you export the actions for this resource according to:
exports.index = function index (req, res) { // GET http://yourdomain.com/horses res.send( MyHorseModel.getAll() ) } exports.show = function show (req, res) { // GET http://yourdomain.com/horses/seabiscuit res.send( MyHorseModel.get(req.params.horse) ) } exports.create = function create (req, res) { // PUT http://yourdomain.com/horses if (app.user.canWrite) { MyHorseModel.put(req.body, function (ok) { res.send(ok) }) } } // ... etc
You can respond with different views:
exports.show = { json: function (req, res) { // GET http://yourdomain/horses/seabiscuit.json } , xml: function (req, res) { // GET http://yourdomain/horses/seabiscuit.xml } }
Middlewares such as express-resource can make life easier with Node and Express, check out the github examples to see if it does what you need.
Luke vivier
source share