Skip to content

Tiny middleware to figure out if a JSON response is expected

License

Notifications You must be signed in to change notification settings

binarykitchen/wants-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Tiny middleware to figure out if a JSON response is expected

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published