nodejs device detection

How to detect device type in Express

So it happened I have developed a REST API Server used by Desktop ( Browser ) and Mobile device, things were working fine till now. Recently I came across a situation where I need to execute the piece of code only for Web browser and mobile browser but not for the mobile App. Custom HTTP header does solve the problem for an instant but was curious to know “Can I find out which kind of device is calling my API ?”. Then i came across this node module “express-device” which does exactly what i wanted.

You can use this module as middleware in Express to provide the type of device in your request object. Here is how to do this using Express.

Creating package.json

Let’s initialize our project using npm init command which will eventually generate the package.json for you.

Tip : Use npm init –yes to skip the questions 😉

Once you have the package.json ready, install the required modules. You need the express and express-device module.

npm i --save express express-device

Node.js device detection

Here is how my package.json.

package.json
{
  "name": "devicedetection",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0",
    "express-device": "^0.4.2"
  }
}

Our Node Server

Let’s built one simple server in Express to accept request on /hello endpoint and respond with Hi to mobile OR web user depending upon request device.

Here is our Server code.

app.js
var express = require('express');
var app = express();
var device = require('express-device');
app.use(device.capture());

app.get('/hello',function(req,res) {
  res.send("Hi to "+req.device.type.toUpperCase()+" User");
});

app.listen(3000);
console.log("Listening to Port 3000");

Upon running this code, if you hit it the server URL ( localhost:3000/hello )from your laptop or desktop. You should be getting this output.

Node.js device

If you hit the same URL via mobile browser, you should be getting the response as shown below.

Nodejs device detection

Additionally, it also detects TV, bot, and tablet. You can access it in the request object. Here is the req.device looks like.

{ parser:
   DeviceParser {
     options:
      { emptyUserAgentDeviceType: 'desktop',
        unknownUserAgentDeviceType: 'phone',
        botUserAgentDeviceType: 'bot',
        carUserAgentDeviceType: 'car',
        parseUserAgent: false },
     make_sure_parser_was_executed: [Function],
     get_model: [Function],
     get_type: [Function] },
  type: 'phone',
  name: '' }

I have only used type key, you can use others as you may need.

Wrapping it up

This is short tip like a tutorial to quickly help you to detect device type in Node.js. I am currently working on few other things hence the frequency of post is getting affected. If anyone of you like to post some of your article or research etc please visit our forum ( Not live right now) and post your article idea or shoot me an email at [email protected].

Shahid (UnixRoot) Shaikh

Hey there, This is Shahid, an Engineer and Blogger from Bombay. I am also an Author and i wrote a programming book on Sails.js, MVC framework for Node.js.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *