https://github.com/nemtsov/express-ensure-ctype
Express middleware for blocking unwanted Content-Type(s)
https://github.com/nemtsov/express-ensure-ctype
Last synced: 4 months ago
JSON representation
Express middleware for blocking unwanted Content-Type(s)
- Host: GitHub
- URL: https://github.com/nemtsov/express-ensure-ctype
- Owner: nemtsov
- License: mit
- Created: 2014-01-20T05:20:35.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2018-01-28T01:13:56.000Z (over 8 years ago)
- Last Synced: 2025-09-03T02:34:54.686Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ensure Content-Type [](http://travis-ci.org/nemtsov/express-ensure-ctype) [](http://badge.fury.io/js/express-ensure-ctype)
Tiny [express](https://github.com/visionmedia/express) middleware for blocking
unwanted Content-Type(s). When a type doesn't match, a 400 error is
sent to the client, otherwise the next middleware is called.
Usage:
```javascript
const express = require('express');
const ensureCtype = require('express-ensure-ctype');
const ensureJson = ensureCtype('json');
const app = express();
app.post('/', ensureJson, (req, res) => {
res.json(req.body);
});
app.listen(3000);
```
Result:
```
curl -i -XPOST http://localhost:3000/
HTTP/1.1 415 Unsupported Media Type
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 37
ETag: W/"25-LXMJEoIT4KTBnB71Rca6CNA/pM0"
Date: Sun, 28 Jan 2018 00:45:38 GMT
Connection: keep-alive
"Unsupported Content-Type. Use: json"
```
```
curl -i -H 'Content-type: application/json' -d '{}' http://localhost:3000/
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
ETag: W/"7-ofL7/ixK2BdJzQOAtzUpXQb50MQ"
Date: Sun, 28 Jan 2018 00:44:58 GMT
Connection: keep-alive
Transfer-Encoding: chunked
```
An array can also be used for multiple content-types:
```javascript
ensureCtype(['csv', 'text']);
```
License
-------
[MIT](/LICENSE)