npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-device

v0.4.2

Published

Browser detection library, built on top of express

Downloads

87,495

Readme

express-device Build Status NPM version

Case you're interested only on the device type detection based on the useragent string and don't need all the express related stuff, then use the device package (https://www.npmjs.com/package/device) which was refactored from express-device for that purpose.

why express-device?

I'm really into node.js and lately I've been playing a lot with it. One of the steps I wanted to take in my learning path was to build a node.js module and published it to npm.

Then I had an idea: why not develop a server side library to mimic the behaviour that Twitter's Bootstrap has in order to identify where a browser is running on a desktop, tablet or phone. This is great for a responsive design, but on the server side.

how it came to be?

First I started to search how I could parse the user-agent string and how to differentiate tablets from smartphones. I found a couple good references, such as:

  • http://www.useragentstring.com/pages/useragentstring.php
  • http://detectmobilebrowsers.com/
  • http://www.quirksmode.org/js/detect.html
  • http://googlewebmastercentral.blogspot.pt/2011/03/mo-better-to-also-detect-mobile-user.html
  • http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3888106/How-Can-I-Detect-the-iPhone--iPads-User-Agent.htm
  • http://windowsteamblog.com/windows_phone/b/wpdev/archive/2011/08/29/introducing-the-ie9-on-windows-phone-mango-user-agent-string.aspx

But then I came across with Brett Jankord's blog. He developed Categorizr which is what I was trying to do for node.js but for PHP. He has the code hosted at Github. So, express-device parsing methods (to extract device type) are based on Categorizr. Also I've used all of his user-agent strings compilation to build my unit tests.

how to use it?

From v0.4.0 express-device only works with express >= v4.x.x and node >= v0.10. To install it you only need to do:

$ npm install express-device

Case you're using express 3.x.x you should install version 0.3.13:

$ npm install [email protected]

Case you're using express 2.x.x you should install version 0.1.2:

$ npm install [email protected]

express-device is built on top of express framework. Here's an example on how to configure express to use it:

var device = require('express-device');

app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.set('views', __dirname + '/views');

app.use(bodyParser());
app.use(device.capture());

By doing this you're enabling the request object to have a property called device, which have the following properties:

Since version 0.3.4 you can now override some options when calling device.capture(). It accepts an object with only the config options (the same that the device supports) you which to override (go here for some examples). The ones you don't override it will use the default values. Here's the list with the available config options:

express-device can also add some variables to the response locals property that will help you to build a responsive design:

Here's an example on how to use them (using EJS view engine):

<html>
<head>
    <title><%= title %></title>
</head>
<body>
    <h1>Hello World!</h1>
    <% if (is_desktop) { %>
    <p>You're using a desktop</p>
    <% } %>
    <% if (is_phone) { %>
    <p>You're using a phone</p>
    <% } %>
    <% if (is_tablet) { %>
    <p>You're using a tablet</p>
    <% } %>
    <% if (is_tv) { %>
    <p>You're using a tv</p>
    <% } %>
    <% if (is_bot) { %>
    <p>You're using a bot</p>
    <% } %>
    <% if (is_car) { %>
    <p>You're using a car</p>
    <% } %>
</body>
</html>

You can check a full working example here.

In version 0.3.0 a cool feature was added: the ability to route to a specific view\layout based on the device type (you must pass the app reference to device.enableViewRouting(app) to set it up). Consider the code below:

.
|-- views
    |-- phone
    |    |-- layout.ejs
    |    `-- index.ejs
    |-- layout.ejs
    `-- index.ejs

And this code:

var device = require('express-device');

app.set('view engine', 'ejs');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');

app.use(bodyParser());
app.use(device.capture());

device.enableViewRouting(app);

app.get('/', function(req, res) {
    res.render('index.ejs');
})

If the request comes from a phone device then the response will render views/phone/index.ejs view with views/phone/layout.ejs as layout. If it comes from another type of device then it will render the default views/index.ejs with the default views/index.ejs. Simply add a folder below your views root with the device type code (phone, tablet, tv or desktop) for the device type overrides. Several combinations are supported. Please check the tests for more examples.

You also have an ignore option:

app.get('/', function(req, res) {
    res.render('index.ejs', { ignoreViewRouting: true });
})

There's a way to force a certain type of device in a specific request. In the example I'm forcing a desktop type and the view rendering engine will ignore the parsed type and render as if it was a desktop that made the request. You can use all the supported device types.

app.get('/', function(req, res) {
    res.render('index.ejs', { forceType: 'desktop' });
})

View routing feature uses the express-partials module for layout detection. If you would like to turn it off, you can use the noPartials option (be advised that by doing this you can no longer use the master\partial layout built into express-device, but you can route to full views):

var device = require('express-device'); 

app.set('view engine', 'ejs');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');

app.use(express.bodyParser());
app.use(device.capture());

device.enableViewRouting(app, {
    "noPartials":true
});

app.get('/', function(req, res) {
    res.render('index.ejs');
})

contributors

where to go from here?

Currently express-device is on version 0.4.2. In order to add more features I'm asking anyone to contribute with some ideas. If you have some of your own please feel free to mention it here.

But I prefer that you make your contribution with some pull requests ;)

license

(The MIT License)

Copyright (c) 2012-2015 Rodrigo Guerreiro

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.