Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/myrotvorets/express-acceptsjson-middleware
Express.js middleware to check whether the client is willing to accept JSON response
https://github.com/myrotvorets/express-acceptsjson-middleware
express-middleware
Last synced: 22 days ago
JSON representation
Express.js middleware to check whether the client is willing to accept JSON response
- Host: GitHub
- URL: https://github.com/myrotvorets/express-acceptsjson-middleware
- Owner: myrotvorets
- License: mit
- Created: 2020-06-08T12:54:55.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-12-16T03:54:48.000Z (26 days ago)
- Last Synced: 2024-12-16T20:36:06.268Z (25 days ago)
- Topics: express-middleware
- Language: TypeScript
- Homepage:
- Size: 2.47 MB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# express-acceptsjson-middleware
Express.js middleware to check whether the client is willing to accept JSON response.
## Usage
```typescript
import { type Request, type Response, Router } from 'express';
import { acceptsJsonMiddleware } from '@myrotvorets/express-acceptsjson-middleware';const router = Router();
router.get('/some-path', acceptsJsonMiddleware, otherHandler);
```## What it Does
The middleware checks the presence of `Accept` HTTP header, and if it is there, it checks whther the client is willing to accept JSON. If not, it returns 406 Not Acceptable error (with JSON payload :-) ).
It is considered that the client is willing to accept JSON if at least one of the following conditions is true:
* there is no `Accept` header;
* `Accept` header contains `*/*` or `*/json`;
* `Accept` header contains `application/*` or `application/json`.The actual check is performed by [Request.accepts()](https://expressjs.com/en/4x/api.html#req.accepts).