# http-ndjson

> Log http requests as ndjson

Latest version **3.1.0** (published 2017-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-ndjson
pnpm add http-ndjson
yarn add http-ndjson
bun add http-ndjson
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2017-11-14 |
| First published | 2015-09-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Maintainers | yoshuawuyts |
| Keywords | http, log, logger, ndjson, standard, request, response, server, development, production, modular, tiny, bole, bistre, garnish |

## Links

- npm: https://www.npmjs.com/package/http-ndjson
- Repository: https://github.com/yoshuawuyts/http-ndjson
- Homepage: https://github.com/yoshuawuyts/http-ndjson#readme
- Issues: https://github.com/yoshuawuyts/http-ndjson/issues
- npm.io page: https://npm.io/package/http-ndjson

## Dependencies (2)

- [xtend](https://npm.io/package/xtend.md) ^4.0.0
- [end-of-stream](https://npm.io/package/end-of-stream.md) ^1.1.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 3.1.0 (latest) — 2017-11-14
- 3.0.0 — 2016-03-04
- 2.3.3 — 2015-10-27
- 2.3.2 — 2015-10-27
- 2.3.1 — 2015-10-27
- 2.3.0 — 2015-10-27
- 2.2.0 — 2015-10-27
- 2.1.0 — 2015-10-27
- 2.0.0 — 2015-10-25
- 1.1.2 — 2015-09-19
- 1.1.1 — 2015-09-15
- 1.1.0 — 2015-09-06
- 1.0.1 — 2015-09-04
- 1.0.0 — 2015-09-04

## README

# http-ndjson
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![Downloads][downloads-image]][downloads-url]
[![js-standard-style][standard-image]][standard-url]

Log http requests as ndjson. Works pretty well with `bole`, so you should
probably use it with that. That is my recommendation.

## Installation
```sh
$ npm install http-ndjson
```

## Usage
```js
const httpNdjson = require('http-ndjson')
const http = require('http')

http.createServer(function (req, res) {
  const setSize = httpNdjson(req, res, console.log)
  const myCoolResponse = 'chickens'
  setSize(myCoolResponse.length)
  res.end(myCoolResponse)
}).listen()
```
```js
{ name: 'http', method: 'GET', message: 'request', url: '/' }
{ name: 'http', method: 'GET', message: 'response', url: '/', statusCode: 200, elapsed: '5ms' }
```

## Log custom properties
`http-ndjson` logs a sensible set of standard properties, but sometimes there's
a need to dive in and log more. An optional third argument can be added with
custom fields that will be logged on either `request` or `response`.
```js
const httpNdjson = require('http-ndjson')
const http = require('http')

http.createServer(function (req, res) {
  const opts = { req: { requestId: req.headers['requestId'] } }
  httpNdjson(req, res, opts, console.log)
  res.end()
}).listen()
```

If `opts.req` or `opts.res` is a function, it will be called and its return value will be used to set custom fields.

## Forward headers
Determining the origin of a request can be hard when using reverse-proxies.
It's not too uncommon for users to mask their IP by providing an
`x-forwarded-for` header. `http-ndjson` makes no assumptions about forwarding
headers and logs all properties instead. The following headers are logged:
- __x-forwarded-for:__ standardized reverse proxy header ([rfc7239][7239])
- __x-real-ip:__ non-standard reverse proxy header
- __http-client-ip:__ non-standard reverse proxy header

## API
### readStream = httpNdjson(req, res, opts?, cb)
Create an http logger. Returns a write stream. Opts can contain the following
values:
- __req:__ an object with values that will be logged on `request`
- __res:__ an object with values that will be logged on `response`
- __opts:__ set options
- __cb:__ handle the returned message

## See Also
- [bole](https://github.com/rvagg/bole)
- [garnish](https://github.com/mattdesl/garnish)
- [ndjson](https://github.com/maxogden/ndjson)

## License
[MIT](https://tldrlegal.com/license/mit-license)

[npm-image]: https://img.shields.io/npm/v/http-ndjson.svg?style=flat-square
[npm-url]: https://npmjs.org/package/http-ndjson
[travis-image]: https://img.shields.io/travis/yoshuawuyts/http-ndjson/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/yoshuawuyts/http-ndjson
[codecov-image]: https://img.shields.io/codecov/c/github/yoshuawuyts/http-ndjson/master.svg?style=flat-square
[codecov-url]: https://codecov.io/github/yoshuawuyts/http-ndjson
[downloads-image]: http://img.shields.io/npm/dm/http-ndjson.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/http-ndjson
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: https://github.com/feross/standard
[7239]: https://tools.ietf.org/html/rfc7239

---
_Source: https://npm.io/package/http-ndjson · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
