Skip to content

Instantly share code, notes, and snippets.

@niksmac
Last active January 17, 2024 08:48
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save niksmac/5ec75829e9359475cbda to your computer and use it in GitHub Desktop.
Save niksmac/5ec75829e9359475cbda to your computer and use it in GitHub Desktop.
Nodejs Express code to redirect mobile user agets
var express = require('express'),
app = express();
app.listen(80);
app.get('/', function(req, res){
var ua = req.header('user-agent');
// Check the user-agent string to identyfy the device.
if(/mobile|iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile|ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(ua)) {
res.sendfile(__dirname + '/mobile.html');
} else {
res.sendfile(__dirname + '/index.html');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment