0.1.9 • Published 10 years ago

ocean-json-api v0.1.9

Weekly downloads
2
License
MIT
Repository
-
Last release
10 years ago

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()

0.1.9

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.0

10 years ago