Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
Last active December 17, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EvanHahn/5596036 to your computer and use it in GitHub Desktop.
Save EvanHahn/5596036 to your computer and use it in GitHub Desktop.
Express server that looks for files in /common, then figures out the useragent and goes somewhere else.
// Load dependencies
var express = require('express');
var is = require('browseris');
var fs = require('fs');
// Build me an app!
var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.compress());
// What are some URLs we should use?
var url = {
common: 'common',
mobile: 'mobile',
desktop: 'desktop'
};
// Show me an app!
app.get('/', function(request, response) {
// Which file to render?
var location;
if (is(request).mobile) {
location = url.mobile + '/app.html';
} else {
location = url.desktop + '/app.html';
}
// Send me a file
response.sendfile(location);
});
// What about everything else?
app.get('*', function(request, response) {
// Look for it in /common
var commonPath = url.common + request.path;
fs.exists(commonPath, function(exists) {
// Which file to render?
var location;
if (exists) {
location = commonPath;
} else {
if (is(request).mobile) {
location = url.mobile + request.path;
} else {
location = url.desktop + request.path;
}
}
// Render that puppy
response.sendfile(location);
});
});
// Start that app
app.listen(app.get('port'));
console.log('Server listening on port ' + app.get('port'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment