项目作者: myrotvorets

项目描述 :
Express.js middleware to check whether the client is willing to accept JSON response
高级语言: TypeScript
项目地址: git://github.com/myrotvorets/express-acceptsjson-middleware.git
创建时间: 2020-06-08T12:54:55Z
项目社区:https://github.com/myrotvorets/express-acceptsjson-middleware

开源协议:MIT License

下载


express-acceptsjson-middleware

Express.js middleware to check whether the client is willing to accept JSON response.

Usage

  1. import { type Request, type Response, Router } from 'express';
  2. import { acceptsJsonMiddleware } from '@myrotvorets/express-acceptsjson-middleware';
  3. const router = Router();
  4. 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().