send-http
v1.0.6
Published
A straightforward way to send data for http.IncomingMessage.
Downloads
318
Maintainers
Readme
send-http
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