http-json-response

send json easy

npm install http-json-response
1 downloads in the last week
13 downloads in the last month

http-json-response

Build Status

makes sending JSON over http a little more convenient

usage

var send = require('http-json-response')
  , http = require('http')

http.createServer(function (req, res) {
  if (req.url === '/good') return send(res, {success: true})
  if (req.url === '/bad') return send(res, 500, {success: false})
  return send(res, 403, {success: 'kinda'})
}).listen(2100)

.. or if you're feeling particularly brazen

var http = require('http')

http.ServerResponse.prototype.send = require('http-json-response')

http.createServer(function (req, res) {
  if (req.url == '/good') return res.send({success: true})
  if (req.url == '/bad') return res.send(500, {success: false})
  return res.send(403, {success: 'kinda'})
}).listen(6666)

...at your own risk, of course.

notes

  • adds in content-type: application/json header for you
  • send automatically calls res.end().
  • If the status code is not specified, it defaults to 200 (OK).

license

MIT

npm loves you