Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Browser detection library for node.js, built on top of express

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.md

express-device Build Status

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:

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?

To install it you only need to do:

$ npm install express-device

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

$ npm install express-device@0.1.1

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

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

    app.use(express.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:

Name Field Type Description Possible Values
type string It gets the device type for the current request desktop, tv, tablet ot phone

express-device also comes with some dynamic helpers that will help you to build a responsive design:

is_desktop It returns true in case the device type is "desktop"; false otherwise
is_phone It returns true in case the device type is "phone"; false otherwise
is_tablet It returns true in case the device type is "tablet"; false otherwise
is_mobile It returns true in case the device type is "phone" or "tablet"; false otherwise
is_tv It returns true in case the device type is "tv"; false otherwise
device_type It returns the device type string parsed from the request

In order to enable this methods you have to call app.enableDeviceHelpers().

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>
    <% } %>
</body>
</html>

You can check a full working example here.

where to go from here?

Currently express-device is on version 0.1.0. There are a couple of things that I have in mind to add, such as:

  • different view rendering based on device type
  • parsing the OS

Feel free to add issues with your own ideas or make pull requests (prefered method).

license

(The MIT License)

Copyright (c) 2012 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.

Something went wrong with that request. Please try again.