Node.js: Browser / User-Agent detection
How to detect browser / User-Agent in Node.js#
Whether you are using a bare bones Node.js HTTP server or Express, the browser user agent information can be found in the User-Agent
HTTP request header.
The request object is popularly named req
, so the user agent information can be found at req.headers['user-agent']
.
With many browsers and versions, browser detection is a very nuanced task. If you are looking for fine control about the details you detect, you should use the ua-parser module.
User-Agent
header can be easily faked.References#
- MDN - HTTP Headers
- Node.js - HTTP request object
- Node.js - HTTP request headers