forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestsOfType.js
27 lines (24 loc) · 848 Bytes
/
requestsOfType.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
express middleware that sends a 406 Unacceptable
response if an incoming request's Content-Type
header does not match `type`
*/
const requestsOfType = (type) => (req, res, next) => {
const hasContentType =
req.get('content-type') !== undefined && req.get('content-type') !== null;
const isCorrectType = req.is(type) === null || req.is(type) === type;
if (hasContentType && !isCorrectType) {
if (process.env.NODE_ENV === 'development') {
console.error(
`Requests with a body must be of Content-Type "${type}". Sending HTTP 406`
);
}
return next({
code: 406,
message: `Requests with a body must be of Content-Type "${type}"`
}); // 406 UNACCEPTABLE
}
return next();
};
export default requestsOfType;
export const requestsOfTypeJSON = () => requestsOfType('application/json');