Socket
Book a DemoInstallSign in
Socket

jsonresponse2

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonresponse2

Uniform JSON response object

latest
Source
npmnpm
Version
0.3.3
Version published
Maintainers
1
Created
Source

GitHub license npm
NPM

jsonresponse

A JSON response helper forked from jsonresponse.

A standard success response would have the format:

{
  "success": true,
  "error": null,
  "results": {
    id: 123,
    firstname: "Brian",
    lastname: "Mancini",
    age: 32
  }
}

An error response would have the format:

{
  "success": false,
  "error": {
    "message": "Code did something bad",
    "stack": "Error: Code did something bad\n    at Object.exports.throwError [as handle] ... "
  },
  "results": null
}

##Usage

Install jsonresponse2 from NPM:

npm install jsonresponse2

You can then include it in a module via:

var jsonresponse2 = require('jsonresponse2')

jsonresponse2

jsonresponse2 function takes three arguments, error, result and stringify.

To create a success response, pass in the results object, array, or value as the second argument:

var results = { foo: 'bar' }
var json = jsonresponse2(null, results)

To create an error response, pass in the error result as the first argument.

var error = new Error('Boom!')
var json = jsonresponse2(error)

You can optionally pass a third argument, a boolean, indicating whether JSON.stringify should be done on the response.

var json = jsonresponse2(null, { foo: 'bar' }, true)

FAQs

Package last updated on 03 Oct 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