Skip to content

JSendie - JSON responses middleware for Express, based on JSend specification.

License

Notifications You must be signed in to change notification settings

kahwooi/jsendie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSendie

Build Status

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.

About

JSendie - JSON responses middleware for Express, based on JSend specification.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published