Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created October 10, 2013 08:36
Show Gist options
  • Save jonathanconway/6915007 to your computer and use it in GitHub Desktop.
Save jonathanconway/6915007 to your computer and use it in GitHub Desktop.
Allows you to customize ExpressJS's view rendering so that you can, say, use views in a 'mobile' folder when user is accessing site through a mobile device.
// dependencies: requirejs, express
define(['express'], function (express) {
return function (app, resolver) {
return function(req, res, next) {
var oldRender = res.render;
res.render = function (view, model) {
return oldRender.call(this, resolver.call(this, req, res) + view, model);
};
next();
};
};
});
// Example:
// If the user is accessing the site from a mobile device, load views from the /mobile subfolder.
// Dependencies: mobile-agent, express
// var viewResolver = function(req, res) {
// if (mobileAgent(req.headers['user-agent']).Mobile) {
// return 'mobile/';
// }
// return '';
// }
// app.use(expressViewResolver(app, viewResolver));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment