Skip to content

Instantly share code, notes, and snippets.

@ayoubkalilcds
Forked from niksmac/detect-mobile-browser.js
Created November 14, 2019 07:51
Show Gist options
  • Save ayoubkalilcds/0494daed04611e3efdcdc423cf517761 to your computer and use it in GitHub Desktop.
Save ayoubkalilcds/0494daed04611e3efdcdc423cf517761 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