Most implementations i\'ve seen are for browser detection on the client side. I was just wondering if it was possible to do browser detection before sending any resources to
Most browsers provide an HTTP request header called "User-Agent" This is the same as the navigator.userAgent property on the client side.
The ua-parser library for node (npm install ua-parser
) exposes a big set of regexes for browser user-agent strings. I'd strongly recommend it for your needs.
I wanted to do a simple redirection to a mobile version of my site, so user-agent is reliable enough. I wanted to do it server-side so I didn't waste time loading unnecessary css and js on the client. http://detectmobilebrowsers.com/ had the most robust regex to match. So I threw together some express middleware that will let you do the redirection by just adding two lines of code to your app.
npm install detectmobilebrowsers
to install
express = require 'express'
mobile = require 'detectmobilebrowsers'
app = express()
app.configure () ->
app.use mobile.redirect 'http://m.domain.com'
app.get '/', (req, res) ->
res.send 'Not on Mobile'
app.listen 3000