node package manager
Painless code sharing. npm Orgs help your team discover, share, and reuse code. Create a free org »

ocean-json-api

Setup routes for json api with Express.

Generated routes:

  • post /path
  • post /path/find
  • get /path/:id?
  • put /path/:id?
  • del /path/:id?

Installation

npm install ocean-json-api

Usage

Basic

 
var OceanApi = require('OceanApi'); 
var api = new OceanApi()
 
//First way 
/**
 *    Model Interface
 *    Model.create(options, cb)
 *    Model.find(options, cb)
 *    Model.update(options, cb)
 *    Model.destroy(options, cb)
 *
 *   OceanApi will automatically generate a Controller 
 *   with interface below.
 */
api.generate('/user', {
  model: Model
})
 
 
//Second way 
/**
 *    Controller Interface (essentially middleware)
 *    Ctrl.create(req, res)
 *    Ctrl.find(req, res)
 *    Ctrl.update(req, res)
 *    Ctrl.destroy(req, res)
 */
api.generate('/person', {
  controller: Ctrl
})
 
api.app.listen(3000, function(){
  console.log("Listening on port 3000"); 
})
 

Pass in or mount an Express app

 
var app = express();
var api = new OceanApi(app)
 
//or 
 
var app = express(); 
app.get('/other', function(req, res, next){
  res.render('index');
})
 
var api = new OceanApi()
api.generate('/user', {
  controller: require('./UserController')
})
 
api.mount(app)
 
 

Where you mount the app matters. If you want the routes after the json api, mount it after you call api.generate()