Have opinions about JavaScript? We want to hear them. Take the 2018 JavaScript Ecosystem Survey »

express-useragent-exposer

2.1.0 • Public • Published

build status

Fast Middleware User-Agent Exposer for NodeJS

express-useragent-exposer is a simple NodeJS/ExpressJS middleware user-agent exposer to your application and views created by Alexey Gordeyev and forked by Angel Custodio.

Installation

Installation is done using the Node Package Manager (npm). If you don't have npm installed on your system you can download it from npmjs.org To install express-useragent-exposer:

    $ npm install express-useragent-exposer --save

Usage overview

Simple Node App

var http = require('http')
  , useragent = require('express-useragent-exposer');
 
var srv = http.createServer(function (req, res) {
  var source = req.headers['user-agent'],
  ua = useragent.parse(source);
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(JSON.stringify(ua));
});
 
srv.listen(3000);

For ExpressJS

var express = require('express');
var app = express();
var useragent = require('express-useragent-exposer');
 
app.use(useragent.express());
app.get('/', function(req, res){
    res.send(req.useragent);
});
app.listen(3000);

Module provides details such as the following:

{
  "isMobile": true,
  "isDesktop": true,
  "isBot": false,
  .....
  "browser": "Chrome",
  "browserversion": "55.0.2883.79",
  "os": "iOS",
  "osversion": "10.2",
  "source": "Mozilla/5.0 (iPad; CPU OS 10_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/55.0.2883.79"
}
 

Accessing the user-agent

If you are using express or connect, then express-useragent-exposer provides an easy way to access the user-agent as:

  • req.useragent from your app server
  • useragent helper accessible from your express views

Client side

  • Clone the repo: git clone git://github.com/angelcustodio/express-useragent-exposer.git
  • Or install with Bower: bower install express-useragent-exposer.

The client side version of express-useragent-exposer available in the /lib subdirectory.

Include file in your HTML – the minimum required for this plugin are:

    <script type="text/javascript" src="express-useragent-exposer.js"></script>

Execute the plugin:

    var userAgent = new UserAgent().parse(navigator.userAgent);

Running tests

Ensure you have nodeunit by running npm install -g nodeunit. Then, run npm test.

npm install
npm test

Run example node app

npm run-script http

Run example express app

npm run-script express

Creator

Alexey Gordeyev (aleksej@gordejev.lv)

Forker

Angel Custodio (ancude@gmail.com)

License

(The MIT License)

Copyright (c) 2012 Alexey Gordeyev aleksej@gordejev.lv

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

install

npm i express-useragent-exposer

Downloadsweekly downloads

539

version

2.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability