wants-json

2.0.1 • Public • Published

wants-json

Build Status

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

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()
    }
})

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i wants-json

    Weekly Downloads

    5

    Version

    2.0.1

    License

    MIT

    Unpacked Size

    6.91 kB

    Total Files

    17

    Last publish

    Collaborators

    • michael.heuberger