https://github.com/kikobeats/send-http
A `res.end` with data type detection.
https://github.com/kikobeats/send-http
Last synced: 4 months ago
JSON representation
A `res.end` with data type detection.
- Host: GitHub
- URL: https://github.com/kikobeats/send-http
- Owner: Kikobeats
- License: mit
- Created: 2022-12-01T15:59:41.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-30T18:03:59.000Z (over 1 year ago)
- Last Synced: 2024-10-17T17:57:44.834Z (6 months ago)
- Language: JavaScript
- Homepage:
- Size: 43.9 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# send-http

[](https://coveralls.io/github/Kikobeats/send-http)
[](https://www.npmjs.org/package/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
```bash
$ npm install send-http --save
```## Usage
```js
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:
```js
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](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/send-http/blob/master/LICENSE.md) License.
Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/send-http/contributors).> [kikobeats.com](https://kikobeats.com) · GitHub [Kiko Beats](https://github.com/Kikobeats) · Twitter [@Kikobeats](https://twitter.com/Kikobeats)