send-http

1.0.6 • Public • Published

send-http

Last version Coverage Status NPM Status

A straightforward way to send data for http.IncomingMessage.

It's like res.send, but:

  • It accepts any kind of value (number, string, object, stream, etc).
  • It checks http.IncomingMessage is writable before write.
  • It determines Content-Type based on the data type.
  • It optionally sets status code as third argument.
  • It's small (~50 LOC).

Install

$ npm install send-http --save

Usage

const send = require('send-http')
const http = require('http')
const got = require('got')

http.createServer((req, res) => {
  /* send with no body */
  send(res, 200)

  /* send a string */
  send(res, 200, 'foo')

  /* send an object */
  send(res, 200, { foo: 'bar' })

  /* send a number */
  send(res, 200, 1234)

  /* send a buffer */
  send(res, 200, Buffer.from('hello world'))

  /* send a stream  */
  send(res, 200, got.stream('https.//example.com'))
})

Additionally, you can .create to customize the behvaior before sending the data:

const send = require('send-http').create((res, data) => {
  if (Buffer.byteLength(data) > 6291456) {
    throw new Error('Payload size is over 6mb')
  }
  return res.end(data)
})

License

send-http © Kiko Beats, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats

Package Sidebar

Install

npm i send-http

Weekly Downloads

48

Version

1.0.6

License

MIT

Unpacked Size

6.79 kB

Total Files

4

Last publish

Collaborators

  • kikobeats