Connect middleware to route based on X-UA-Device
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
.editorconfig
.gitignore
.travis.yml
README.md
connect_device_router.js
package.json

README.md

connect-device-router NPM version Build Status

Connect middleware to route based on X-UA-Device.

Use with varnish-devicedetect or connect-devicedetect to generate X-UA-Device headers.

Querystring overrides require connect.query or similar.

Read about how we use it here.

connect = require 'connect'
deviceDetect = require 'connect-devicedetect'
device = require 'connect-device-router'

desktopApp = connect()
  .use( ... )

mobileApp = connect()
  .use( ... )

app = connect()
  .use(connect.query())
  .use(deviceDetect())
  .use(device(phone: mobileApp))
  .use(desktopApp)

Or use per-route with express:

express = require 'express'
deviceRouter = require 'connect-device-router'

express()
  # map devices to handlers with chained middleware:
  .get '/foo',
    device 'phone', (req, res, next) ->
      # ...
    device 'desktop', (req, res, next) ->
      # ...
    (req, res, next) ->
      # default

  # or an object literal:
  .get '/', device
    phone: (req, res, next) -> # ...
    tablet: (req, res, next) -> # ...
  , (req, res, next) -> # default ...

  # or mix both styles:
  .get '/',
    device phone: (req, res, next) ->
      # ...
    device tablet: (req, res, next) ->
      # ...
    (req, res, next) ->
      # default ...