# http-json

> An extension of the http prototype supporting steve-style json responses in connect

Latest version **1.2.2** (published 2012-12-20) · 0 weekly downloads

## Install

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

## 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 | 1.2.2 |
| Published | 2012-12-20 |
| First published | 2012-06-03 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | AJ ONeal |
| Maintainers | coolaj86 |

## Links

- npm: https://www.npmjs.com/package/http-json
- Repository: https://github.com/coolaj86/steve
- Homepage: http://github.com/coolaj86/steve
- npm.io page: https://npm.io/package/http-json

## Recent versions

- 1.2.2 (latest) — 2012-12-20
- 1.2.1 — 2012-10-11
- 1.1.0 — 2012-08-17
- 1.0.2 — 2012-06-14
- 1.0.1 — 2012-06-03
- 1.0.0 — 2012-06-03

## README

http-json
===

A `connect` / `http` module which extends `http.ServerResponse.prototype`.

Usage
===

    (function () {
      "use strict";

      require('http-json').init(require('http'));

      var connect = require('connect')
        ;

      connect().use(function () {
        res.meta('foo-secret', 42);

        // Randomly generate an error
        if (Date.now() % 3) {
          res.error(new Error("What time is it? Error time!"));
          res.json();
          return;
        } else {
          res.warn(new Error("Did you know that 1 out of 3 times is error time?"));
        }

        res.json("What time is it? Success time!");
      }).listen(2323);
      // http://localhost:2323/

    }());

Response
---

    {
        "success": true
      , "errors": []
      , "warnings": [{"message":"Did you know that 1 out of 3 times is error time?"}]
      , "foo-secret": 42
      , "timestamp": 1339631260781
      , "result": "What time is it? Success time!"
    }

API
===

http.ServerResponse.prototype.json(result, params)
---

Pass in some `result` object to be attached as `result`

  * sets `Content-Type: application/json; charset=utf-8`
  * calls `res.write(JSON.stringify({ timestamp: Date.now(), result: result, ... }))`
  * calls `res.end()`

Optional `params` may be an object such as this

    {
        debug: false        // set `true` if the response should be pretty-printed
                            // also, stack traces should not be removed
      , stringified: false  // set `true` if the result is already `JSON.stringify()`'d
    }

http.ServerResponse.prototype.meta(key, value)
---

Add a field to the "header" (sibling of `result`, `errors`, etc)

http.ServerResponse.prototype.warn(Error)
---

Add an Error to the `warnings` array, but `success` is left as true.

`Error` may also be `null`/`undefined` (ignored), an array of Errors, or an empty array (ignored).

**Note**: By default, stack traces will be removed from warnings.

http.ServerResponse.prototype.error(Error)
---

Add an Error to the `errors` array and set `success` to false.

`Error` may also be `null`/`undefined` (ignored), an array of Errors, or an empty array (ignored).

**Note**: By default, stack traces will be removed from errors.

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