Package Exports
- express-useragent
- express-useragent/browser
- express-useragent/browser/min
- express-useragent/package.json
Readme
express-useragent
Fast user-agent parser with first-class Express middleware and TypeScript typings. Works server-side in Node.js and in the browser via a lightweight IIFE bundle.
Requires Node.js 18 or newer.
Install
npm install express-useragentQuick Start
import http from 'node:http';
import { UserAgent } from 'express-useragent';
const server = http.createServer((req, res) => {
const source = req.headers['user-agent'] ?? 'unknown';
const parser = new UserAgent().hydrate(source);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(parser.Agent));
});
server.listen(3000);Express Middleware
import express from 'express';
import useragent from 'express-useragent';
const app = express();
app.use(useragent.express());
app.get('/', (req, res) => {
res.json({
browser: req.useragent?.browser,
os: req.useragent?.os,
});
});
app.listen(3000);API Highlights
new UserAgent()— build a fresh parser instance.useragent.parse(source)— quick parse returning the agent snapshot.useragent.express()— Express-compatible middleware that hydratesreq.useragentandres.locals.useragent.parser.Agent— normalized fingerprint with convenience booleans (isMobile,isBot, etc.).
Sample payload:
{
"isMobile": false,
"isDesktop": true,
"isBot": false,
"browser": "Chrome",
"version": "118.0.0",
"os": "macOS Sonoma",
"platform": "Apple Mac",
"source": "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0)..."
}Browser Usage
The build exports drop-in browser bundles under dist/browser/:
express-useragent.global.js— readable IIFE that exposeswindow.UserAgentandwindow.useragent.express-useragent.global.min.js— minified version of the same API.
<script src="/vendor/express-useragent.global.min.js"></script>
<script>
const agent = new UserAgent().parse(navigator.userAgent);
console.log(agent.browser, agent.version);
</script>Prefer consuming the ESM/CJS entry from your bundler when possible:
import { UserAgent } from 'express-useragent';
const agent = new UserAgent().parse(navigator.userAgent);Scripts
npm install # install dependencies
npm run lint # lint the TypeScript sources, tests, and examples
npm run typecheck # run the TypeScript compiler in noEmit mode
npm test # execute Vitest (includes adapted legacy suites)
npm run build # emit dist/ (CJS, ESM, d.ts, browser bundles)Examples for manual testing:
npm run http # raw Node HTTP sample
npm run express # Express middleware demo
npm run simple # CLI parsing helperContributing
Bug reports and PRs are welcome. When submitting changes, please include:
- Updated tests under
tests/covering new parsing behaviour. npm testandnpm run lintoutput or reproduction steps.- Notes in the changelog for breaking updates.
License
MIT © Aleksejs Gordejevs and contributors.