Skip to content

Instantly share code, notes, and snippets.

@vinitkumar
Forked from madhums/express.mobile.js
Created June 14, 2013 05:55
Show Gist options
  • Save vinitkumar/5779755 to your computer and use it in GitHub Desktop.
Save vinitkumar/5779755 to your computer and use it in GitHub Desktop.
// Render view helper
// if the request is coming from mobile and if you have a view
// with users.mobile.jade, then that view will be rendered
app.use(function (req, res, next) {
res._render = res.render
res.render = function (template, locals, cb) {
var ua = req.header('user-agent')
var fs = require('fs')
var view = app.get('views') + '/' + template + '.mobile.jade'
if (/mobile/i.test(ua) && fs.existsSync(view)) {
res._render(template + '.mobile', locals, cb)
} else {
res._render(template, locals, cb)
}
}
next()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment