https://github.com/binarykitchen/wants-json
Tiny middleware to figure out if a JSON response is expected
https://github.com/binarykitchen/wants-json
Last synced: about 1 year ago
JSON representation
Tiny middleware to figure out if a JSON response is expected
- Host: GitHub
- URL: https://github.com/binarykitchen/wants-json
- Owner: binarykitchen
- License: other
- Created: 2015-01-01T03:36:55.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-03-26T10:26:30.000Z (about 2 years ago)
- Last Synced: 2025-02-09T08:34:38.410Z (over 1 year ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# wants-json
[](https://travis-ci.org/binarykitchen/wants-json)
A tiny ExpressJS middleware to figure out if a JSON response is expected.
When you have a single page app, you probably communicate in JSON with the server. But on the first page load you send the HTML. With this middleware you can distinct between these two requests.
## ExpressJS middleware example for single page apps
```js
var wantsJson = require('wants-json'),
express = require('express'),
app = express()
app.use(wantsJson())
app.get('/data', function(req, res, ext) {
if (req.wantsJson()) {
// load data and send to client
} else {
// send single page app
next && next()
}
})
```