# express-json-handler

> Simplify sending JSON responses in express handlers

Latest version **0.0.1** (published 2020-11-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install express-json-handler
pnpm add express-json-handler
yarn add express-json-handler
bun add express-json-handler
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2020-11-12 |
| First published | 2020-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Egor Egorov |
| Maintainers | egorfine |
| Keywords | express, middleware, json, handler, route, connect |

## Links

- npm: https://www.npmjs.com/package/express-json-handler
- Repository: https://github.com/egorFiNE/express-json-handler
- Homepage: https://github.com/egorFiNE/express-json-handler#readme
- Issues: https://github.com/egorFiNE/express-json-handler/issues
- npm.io page: https://npm.io/package/express-json-handler

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.1 (latest) — 2020-11-12

## README

# express-json-handler

Simply return objects from your route handler functions in express.

Instead of:

```javascript
app.post('/auth/', (req, res) => {
  res.json({ success: false });
});
```

do:

```javascript
const JSONHandler = require('express-json-handler');

app.post('/auth/', JSONHandler(async (req, res) {
  return { success: false };
}));
```

## Benefits?

* `return` instead of `res.json()`
* `async` functions properly handled
* errors properly delivered in JSON responses
* browser cache disabled

## Installation

```bash
npm install express-json-handler
```

# Async functions handled and errors catched

express-json-handler properly supports async functions in Express 4. This won't work as expected in Express 4:

```javascript
app.post('/auth/', async (req, res) {
  non_existing();
});
```

express-json-handler catches errors on your async handler and in that case `{ success: false }` is returned. You can change the default reply:

```javascript
const JSONHandler = require('express-json-handler');

JSONHandler.DEFAULT_FAIL_RESPONSE = {
  message: "Internal server error"
};

app.post('/auth/', JSONHandler(async (req, res) {
  non_existing();
}));
```

# Direct responses

You can still send direct responses from your handler code, either with `res.send()` or even `res.json()` if you like. Return nothing or null from your handler in case you do.

# Caching of responses

By default express-json-handler sends cache disabling headers in responses, namely:

```
Cache-Control: private, max-age=0, no-cache, must-revalidate
Pragma: no-cache
```

You can change that behavior:

```javascript
const JSONHandler = require('JSONHandler');

JSONHandler.DEFAULT_HEADERS = {
  'Cache-Control': 'max-age=3600'
};
```

# Development and maintenance

Tests: `npm run test` or `mocha test.js`.

Liner: `npm run lint` or `eslint index.js`.

# Footer

Author: Egor Egorov me@egorfine.com.<br>
License: MIT

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