mpm.express.rest
Rest Framework for Express
mpm.express.rest helps developpers write restfull controllers for expressjs. With mpm.express.rest, developpers no longer needs to write glue code to link the database to express routes.
author: mparaiso mparaiso@online.fr licence: LGPL version:0.0.5
REQUIREMENTS
nodejs , npm , expressjs
Database support through builtin adapters
- Mongoose ( mongoose package required)
- MongoDB ( mongodb package required)
- Javascript Arrays
INSTALLATION
npm install -g mpm.express.rest
USAGE
BASIC USAGE
Exemple with MongoDB
Create a express app with a json parser
var appexpress; express=; app=; app;
Create a mongodb connection , requires a mongodb database ( on localhost here )
var MongoClientdbcollection; MongoClient=MongoClient; MongoClient;
Create the restfull controller and the adapter for MongoDB
var donecontrolleradapterresthttp; http = ; rest = ; { /** * pass the express app into the constructor * you can allow or forbid some verbs by passing an option object * with a allows object : * @example : * new rest.Controller(app,{allow:['list','get','post','put','delete']}); * if no allows option , all verbs and methods are allowed. if you want * a readonly restfull controller : * new rest.Controller(app,{allow:['list','get']}); */ controller = app; // set the adapter , the MongoDBAdapter needs a MongoDB Collection object controller // call controller.handle to create all the routes needs for our rest api http; }
The following routes have been created
GET / list resources,query parameters can be passed to the db adapter for filtering,ect...
GET /:id read
POST / create
PUT /:id update a resource
DELETE /:id delete
or pass the express app into another express app
//main express app var mainapp = ; { //pass the express app into the constructor controller = app; // set the adapter , the MongoDBAdapter needs a MongoDB Collection object controller // call controller.handle to create all the routes needed for our rest api mainapp http; }
The following routes have been created
GET /api/documents
GET /api/documents/:id
POST /api/documents/
PUT /api/documents/:id
DELETE /api/documents/:id
And Voila! You can of course create multiple rest controllers with the same or different DB adapters.
Using Mongoose
var appexpresshttpmongooseSchema Modelcontrolleradapterrest; http = ; rest = ; mongoose=; express=; app=; app; mongoose; Schema = mongoose; Model = mongoose; controller = ; controller; app; http;
Changelog
- 0.0.5 allows option is now allow
TODO
- implement ETAG support
- implement resource nesting (ex :/foo/:id/bar )