🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

proto-jsend

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proto-jsend

jSend middleware for express

latest
Source
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

jSend

Build
Status Coverage
Status

Express middleware for jSend responses.

install

With npm do:

npm install proto-jsend

Then assign as express middleware. This will add the jSend methods to the express request and response objects.

var app = express(),
 jSend = require('proto-jsend');

app.use(jSend);

Methods

res.jSend

/**
 * Send jSend success response to the client
 * 
 * res.jSend will infer the status code (200 or 201 from the request http method)
 * if jsonp is used req.method is always 'GET' so in this case req._method property must be set to the desired method e.g 'POST' if you wish to receive a 201 code
 *
 * @param {Object} data - data to send in response
 */
res.jSend = function (data) {
  // ...
}

res.jSend.error

  • This is for 5xx type errors (i.e. internal server error)
/**
 * Send custom jSend "error" messages to client
 *
 * @param {Object} arguments - Object containing options for jSend.error
 * @param {Number} arguments.code - HTTP Status Code 5xx (defaults to 500)
 * @param {String} arguments.message - Message to be sent to the client
 * @param {Object} [arguments.data] - Object to be sent to the client
 */
res.jSend.error = function (arguments) {
  // ...
}
  • Example usage:
res.jSend.error({
  code: 500,
  message: "Internal Server Error",
  data: new Error('Broke it!')
});
HTTP/1.1 500 Error
{
  "status": "error",
  "message": "Internal Server Error"
}
  • A stack trace (or I guess, whatever you want to put in data) will also show on the data property when not in production environment for debugging purposes:
HTTP/1.1 500 Error
{
  "status": "error",
  "message": "Internal Server Error",
  "data": "Error: Broke it!\n    at Object.<anonymous> (/Users/foo/app.js:12:25)\n    at Module._compile (module.js:460:26)\n    at Object.Module._extensions..js (module.js:478:10)\n    at Module.load (module.js:355:32)\n    at Function.Module._load (module.js:310:12)\n    at Function.Module.runMain (module.js:501:10)\n    at startup (node.js:129:16)\n    at node.js:814:3"
}

res.jSend.fail

  • This is for 4xx type errors (i.e. bad request, unauthorised etc.)
  • Footprint:
/**
 * Send custom jSend "fail" messages to client
 *
 * @param {Object} arguments - Object containing options for jSend.fail
 * @param {Number} arguments.code - HTTP Status Code 4xx (defailts to 400)
 * @param {Object} arguments.data - Object to be sent to the Client
 */
res.jSend.fail = function (arguments) {
   // ...
};
  • Example usage:
res.jSend.error({
  code: 400,
  message: "Bad Request",
  data: {
    title: "Title property of type string is required"
  }
});
HTTP/1.1 400 Bad Request
{
  "status": "fail",
  "data": {
    "title": "Title property of type string is required"
  }
}

FAQs

Package last updated on 24 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts