0.0.2 • Published 8 years ago

jsendie v0.0.2

Weekly downloads
7
License
MIT
Repository
github
Last release
8 years ago

JSendie

JSendie is an Express middleware to provide structured JSON reponses with 3 new methods - res.success, res.fail, and res.error. It is based on JSend specification (http://labs.omniti.com/labs/jsend).

Installation

npm install jsendie

Setup the middleware in your Express app. Before any routes.

var express = require('express')
  , JSendie = require('jsendie')
  , app = express()
  , PORT = 3000;

// Remmeber to use it before any routes
app.use(jsendie());

app.get('/success', function (req, res){
  res.success({
    message: 'Hi, JSendie!'
  });
});

app.listen(PORT, function(){
    console.log('JSendie listen to port: ' + PORT)
});

// http://localhost:3000/success response body
// {
//   "status": "success",
//   "data": {
//     "notice": "Hi, JSendie!"
//   }
// }

Sample

https://github.com/kahwooi/jsendie/blob/master/test/server.js

API

JSendie adds the following methods to the res methods:

res.success(data)

  • data: Required success data.

res.fail(data)

  • data: Required failure data. If the reasons for failure correspond to POST values, the response object's keys SHOULD correspond to those POST values.

res.error(message, code, data)

  • message: Required error message.
  • data: Optional error data.
  • code: Optional error code.

License

This project is licensed under the MIT license. See the LICENSE file for more info.