Last active
December 17, 2015 10:39
-
-
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.
This file contains hidden or 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
// 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