1.0.6 • Published 7 months ago

send-http v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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

1.0.6

7 months ago

1.0.5

8 months ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.0

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago