Last active
December 18, 2015 11:59
-
-
Save madhums/5779739 to your computer and use it in GitHub Desktop.
render mobile layouts using express.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Render mobile views | |
// 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 = template + '.mobile.' + app.get('view engine') | |
var file = app.get('views') + '/' + view | |
// check if the request was from the mobile - thanks to https://gist.github.com/christopherdebeer/1200142 | |
if (/mobile/i.test(ua) && fs.existsSync(file)) { | |
res._render(view, 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
for implementation details checkout madhums/node-express-mongoose-demo#39