Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle user-agent in nodejs environment?

I start to using the package "ua-parser" but the creator is too busy to mantain or commit... the npm ua-parser is outdate, and need to download directly from github. Someone know about other good package like ua-parser that is updated and can be used with expressjs? Or have a way to handle just with expressjs?

like image 458
Undefined Behavior Avatar asked Mar 09 '14 18:03

Undefined Behavior


People also ask

What is user agent in node JS?

User-Agents is a JavaScript package for generating random User Agents based on how frequently they're used in the wild. A new version of the package is automatically released every day, so the data is always up to date.

What is a user agent string?

A browser's User-Agent string (UA) helps identify which browser is being used, what version, and on which operating system. When feature detection APIs are not available, use the UA to customize behavior or content to specific browser versions.

How many user agents are there?

Browse our database of 219.4 million User Agents - WhatIsMyBrowser.com.

What is Express device?

The express device is a temporary storage device shipped to you by IDrive. Your data is protected during transfer and storage using 256-bit AES encryption, with an optional private key. To perform an immediate express backup of your files and folders, Go to the 'Express Backup' tab in the Netgear Backup app page.


2 Answers

Have you looked at:

  • https://github.com/biggora/express-useragent

Or, write your own middleware:

app.use(function(req, res, next) {   res.locals.ua = req.get('User-Agent');   next(); }); 

Reference: get user agent from inside jade

like image 168
tpae Avatar answered Oct 07 '22 02:10

tpae


There are two general situations, where you just need simple matching, and you don't need a module for this, you can just use regex in Node.

var isIpad = !!req.headers['user-agent'].match(/iPad/); var isAndroid = !!req.headers['user-agent'].match(/Android/);  ==> true, false 

The other, if you need a nice clean output of browser type, this worked best for me. https://www.npmjs.org/package/useragent

like image 38
John Williams Avatar answered Oct 07 '22 02:10

John Williams