Server side browser detection? node.js

Most of the implementations I've seen are for client-side browser discovery. I'm just wondering if I was able to discover the browser before sending any resources to the client.

Thank.

+51
javascript browser browser-detection
May 28 '11 at 17:47
source share
14 answers
var ua = request.headers['user-agent'], $ = {}; if (/mobile/i.test(ua)) $.Mobile = true; if (/like Mac OS X/.test(ua)) { $.iOS = /CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.'); $.iPhone = /iPhone/.test(ua); $.iPad = /iPad/.test(ua); } if (/Android/.test(ua)) $.Android = /Android ([0-9\.]+)[\);]/.exec(ua)[1]; if (/webOS\//.test(ua)) $.webOS = /webOS\/([0-9\.]+)[\);]/.exec(ua)[1]; if (/(Intel|PPC) Mac OS X/.test(ua)) $.Mac = /(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/.exec(ua)[2].replace(/_/g, '.') || true; if (/Windows NT/.test(ua)) $.Windows = /Windows NT ([0-9\._]+)[\);]/.exec(ua)[1]; 

This should work for you. Just put it in the response handler.

+77
May 28 '11 at 18:10
source share

The ua-parser library for node ( npm install ua-parser ) provides a large set of regular expressions for browser user agent strings. I highly recommend it for your needs.

+28
Sep 29 '11 at 17:09
source share

I threw this out together using ua-parser-js . I am sure that it can be improved, but it is functional.

Install Package:

 sudo npm install ua-parser-js 

Your routes file requires UAParser:

 var UAParser = require('ua-parser-js'); 

Do a few things with it:

 function ensureLatestBrowser(req, res, next) { var parser = new UAParser(); var ua = req.headers['user-agent']; var browserName = parser.setUA(ua).getBrowser().name; var fullBrowserVersion = parser.setUA(ua).getBrowser().version; var browserVersion = fullBrowserVersion.split(".",1).toString(); var browserVersionNumber = Number(browserVersion); if (browserName == 'IE' && browserVersion <= 9) res.redirect('/update/'); else if (browserName == 'Firefox' && browserVersion <= 24) res.redirect('/update/'); else if (browserName == 'Chrome' && browserVersion <= 29) res.redirect('/update/'); else if (browserName == 'Canary' && browserVersion <= 32) res.redirect('/update/'); else if (browserName == 'Safari' && browserVersion <= 5) res.redirect('/update/'); else if (browserName == 'Opera' && browserVersion <= 16) res.redirect('/update/'); else return next(); } 

and then on your route just call:

 app.all(/^(?!(\/update)).*$/, ensureLatestBrowser); 

If you want to know what other information you can get with UAParser, check out the demo page.

+13
Dec 31 '14 at 20:44
source share

I wanted to make a simple redirect to the mobile version of my site, so the user agent is quite reliable. I wanted to do this on the server side, so I did not waste time downloading unnecessary css and js on the client. http://detectmobilebrowsers.com/ was the most reliable regular expression for matching. So I put together some middleware that will allow you to redirect just by adding two lines of code to your application.

npm install detectmobilebrowsers to install

 express = require 'express' mobile = require 'detectmobilebrowsers' app = express() app.configure () -> app.use mobile.redirect 'http://m.domain.com' app.get '/', (req, res) -> res.send 'Not on Mobile' app.listen 3000 
+4
Aug 13 2018-12-12T00:
source share
 ua = request.headers['user-agent']; if( /firefox/i.test(ua) ) browser = 'firefox'; else if( /chrome/i.test(ua) ) browser = 'chrome'; else if( /safari/i.test(ua) ) browser = 'safari'; else if( /msie/i.test(ua) ) browser = 'msie'; else browser = 'unknown'; 
+3
Aug 01 '13 at 2:07
source share

If you use express, you can easily check ua like this:

 app.get('/ua', function(req, res){ res.send('user ' + req.headers['user-agent']); }); 
+2
Apr 27 2018-12-12T00:
source share

Most browsers provide an HTTP request header called "User-Agent". This is the same as the client-side navigator.userAgent property.

+1
May 28 '11 at 18:01
source share
+1
Jul 14 '11 at 23:19
source share
+1
Feb 10 '12 at 23:28
source share

I recently released the JS detector device a couple of months ago.

This is the TypeScript port of the Matomo device discovery device, a powerful device discovery library originally written in PHP.

He can analyze any user agent and determine the browser, operating system, device used (desktop, tablet, mobile phone, TV, cars, console, etc.), make and model.

It has been thoroughly tested and uses over 6,000 tests to detect thousands of different devices.

Mounting

 npm install device-detector-js 

An example is a simple user agent discovery:

 const DeviceDetector = require("device-detector-js"); const deviceDetector = new DeviceDetector(); const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36"; const device = deviceDetector.parse(userAgent); console.log(device); 

Check out the full API documentation .

+1
Dec 11 '18 at 20:37
source share

If you want to control the mobile device at the template level, I just wrote a module for this. https://github.com/Fresheyeball/isMobile-node

0
Apr 6 '13 at 18:06
source share

I improved the @ bit5 duckce bit code to be really useful and support IE 10-12 (Edge).

 var getDevice = function(ua) { var $ = {active: false, subactive: false}; if (/mobile/i.test(ua)) { $.active = 'mobile'; $.Mobile = true; } if (/like Mac OS X/.test(ua)) { $.active = 'iOS'; $.iOS = /CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.'); if (/like Mac OS X/.test(ua)) { $.subactive = 'iPhone'; $.iPhone = /iPhone/.test(ua); } if (/like Mac OS X/.test(ua)) { $.subactive = 'iPad'; $.iPad = /iPad/.test(ua); } } if (/Android/.test(ua)) { $.active = 'Android'; $.Android = /Android ([0-9\.]+)[\);]/.exec(ua)[1]; } if (/webOS\//.test(ua)) { $.active = 'webOS'; $.webOS = /webOS\/([0-9\.]+)[\);]/.exec(ua)[1]; } if (/(Intel|PPC) Mac OS X/.test(ua)) { $.active = 'Safari'; $.Safari = /(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/.exec(ua)[2].replace(/_/g, '.') || true; } if (/Windows NT/.test(ua)) { $.active = 'IE'; $.IE = /Windows NT ([0-9\._]+)[\);]/.exec(ua)[1]; } if (/MSIE/.test(ua)) { $.active = 'IE'; $.IE = /MSIE ([0-9]+[\.0-9]*)/.exec(ua)[1]; } if (/Trident/.test(ua)) { $.active = 'IE'; $.IE = /Trident\/.*rv:([0-9]+[\.0-9]*)/.exec(ua)[1]; } if (/Edge\/\d+/.test(ua)) { $.active = 'IE Edge'; $.IE = /Edge\/(\d+)/.exec(ua)[1]; } return $.active + ' ' + $[$.active] + ($.subactive && ' ' + $.subactive + ' ' + $[$.subactive]); }; 
0
Oct 27 '15 at 6:53
source share

Perhaps you should take a look at the Apache DeviceMap .

JavaScript libraries out of the box are more on the client side right now, but a lot will work on Node.JS or Angular in a similar way. Unlike simply matching UA string patterns, DeviceMap comes with a wide range of devices and device families in the Device Description Repository (DDR) based on W3C standards.

0
Jan 13 '16 at 14:40
source share

Powerful npm useragent . Useragent allows you to accurately analyze the user agent string using specially configured regular expressions for matching in the browser. This database is necessary to ensure proper analysis of each browser, as each browser provider implements its own user agent scheme. This is why regular user agent parsers have serious problems because they are likely to analyze the wrong browser name or confuse the version of the rendering engine with the real version of the browser.

0
Jan 28 '19 at 10:51
source share



All Articles