Express User-Agent Parser

2.1.0 · active · verified Wed Apr 22

express-useragent is a robust and fast user-agent parsing library designed for Node.js environments, offering dedicated Express.js middleware and comprehensive TypeScript typings. Currently stable at version 2.1.0, the package underwent a significant rewrite in version 2.0.0, migrating to ES Modules and TypeScript, and now requires Node.js 18 or newer. Its release cadence appears active, with recent patches addressing bug fixes and dependency updates. Key differentiators include its first-class integration with Express, which populates `req.useragent` with parsed data, and its ability to parse user-agent strings directly via a `UserAgent` class instance. It also provides lightweight browser bundles for client-side parsing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `express-useragent` middleware into an Express.js application to parse the user-agent string and expose the parsed data on `req.useragent` for easy access within route handlers.

import express from 'express';
import { express as useragent } from 'express-useragent';

const app = express();

// Apply the user-agent middleware
app.use(useragent());

// Define a route to display parsed user-agent data
app.get('/', (req, res) => {
  if (!req.useragent) {
    return res.status(500).json({ error: 'User-agent data not found' });
  }
  res.json({
    browser: req.useragent.browser,
    os: req.useragent.os,
    isMobile: req.useragent.isMobile,
    platform: req.useragent.platform,
    source: req.useragent.source
  });
});

const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

view raw JSON →