Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Node.js Express - Mobile detection
app.get('/', function(req, res){
var ua = req.header('user-agent');
if(/mobile/i.test(ua)) {
res.render('mobile.html');
} else {
res.render('desktop.html');
}
});
@srirangan

This comment has been minimized.

Copy link

@srirangan srirangan commented Jan 25, 2012

Perfect, thanks!

@pribeh

This comment has been minimized.

Copy link

@pribeh pribeh commented Jan 31, 2013

Thanks a bunch for the snippet. Any way to do detection beyond simply mobile via express? Say, phone vs tablet?

@zwigby

This comment has been minimized.

Copy link

@zwigby zwigby commented May 19, 2013

You could break out the user agent even more to tell you more specifically what OS. Similar to detection like, http://www.labnol.org/tech/ipad-user-agent-string/13230/.

@jm921106

This comment has been minimized.

Copy link

@jm921106 jm921106 commented Sep 21, 2016

where is /mobile/i.test() from ???

javascript ? jquery ?

@gandhirahul

This comment has been minimized.

Copy link

@gandhirahul gandhirahul commented Aug 31, 2017

Simple and Precise. Thanks :)
@jm921106 That is native javascript.You can read more about it here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.