Check out the latest documentation.

Using the API in HTTP & Express modules

When used in an HTTP or Exrpess server the request object can be used. Using this method means that the match object is closed when the request ends so there is no need to implement match.close(). The match object is also attached to the request as fiftyoneDevice when getMatch() is called so can be accessed in one of two ways:

The same as with a match outside of an HTTP server

								
var match = provider.getMatch(request);
var isMobile = match.IsMobile;

								

Through the request object

								
provider.getMatch(request);
var isMobile = request.fiftyoneDevice.IsMobile;

								

There are simple HTTP and express server examples in the GitHub repository . These can be run with

								
$ node examples/client.js

								

and

								
$ node examples/expressClient.js

								

The HTTP server example initialises the provider before starting an HTTP server and printing the device properties

								
var http = require("http");
var fiftyonedegrees = require("fiftyonedegreescore");
var config = {};

// Get the path to a Lite data file.
if (process.argv[2] === "pattern") {
    config.dataFile = require('fiftyonedegreeslitepattern');
    console.log("Starting new Pattern provider.")
}
else if (process.argv[2] === "trie") {
    config.dataFile = require('fiftyonedegreeslitetrie');
    console.log("Starting new Trie provider.")
}
else {
    config.dataFile = require('fiftyonedegreeslitepattern');
    console.log("Starting new Pattern provider (default).");
}

// Initialise a new Provider.
var provider = new fiftyonedegrees.provider(config);

var server = http.createServer(function (req, res) {
    // Get a match.
    provider.getMatch(req);

    // Print the type of device.
    if (req.fiftyoneDevice.IsMobile) {
        res.write("This is a mobile device.\n");
    }
    else {
        res.write("This is a non-mobile device.\n");
    }

    // Print all the properties for the device.
    res.write("Here are all its properties:\n\n");
    provider.availableProperties.forEach(function(property) {
        res.write(property + " : " + req.fiftyoneDevice[property] + "\n");
    })
    res.end();
}).listen(3000, function () {
    console.log("Test HTTP server listening on port 3000");
});

								

Express works in exactly the same way. In this example though, a metrics page has been added which prints the match metrics for the device accessing the page.

								
var express = require("express");
var app = express();
var fiftyonedegrees = require("fiftyonedegreescore");

var config = {};

// Get the path to a Lite data file.
if (process.argv[2] === "pattern") {
    config.dataFile = require('fiftyonedegreeslitepattern');
    console.log("Starting new Pattern provider.")
}
else if (process.argv[2] === "trie") {
    config.dataFile = require('fiftyonedegreeslitetrie');
    console.log("Starting new Trie provider.")
}
else {
    config.dataFile = require('fiftyonedegreeslitepattern');
    console.log("Starting new Pattern provider (default).");
}

// Initialise a new Provider.
var provider = new fiftyonedegrees.provider(config);

app.get('/', function(req, res) {
    var deviceString = '';
    
    // Get a match.
    provider.getMatch(req);
    
    // Print the type of device.
    if (req.fiftyoneDevice.IsMobile) {
        deviceString += "This is a mobile device.
\n";
    } else {
        deviceString += "This is a non-mobile device.
\n";
    }
    // Print all the properties for the device.
    deviceString += "Here are all its properties:

\n\n";
    provider.availableProperties.forEach(function(property) {
        deviceString += property + " : " + req.fiftyoneDevice[property] + "
\n";
    })

    res.send(deviceString);
})

app.get('/metrics', function(req, res) {
    if (provider.getDataSetName() === "Trie") {
        // Trie does not support match metrics.
        res.send("Match metrics not available in Trie.
\n");
    } else {
        var metricsString = '';
        // Get a match.
        provider.getMatch(req);

        // Print the match metrics.
        metricsString += "Device Id : " + req.fiftyoneDevice.Id + "
\n";
        metricsString += "Method : " + req.fiftyoneDevice.Method + "
\n";
        metricsString += "Difference : " + req.fiftyoneDevice.Difference + "
\n";
        metricsString += "Rank : " + req.fiftyoneDevice.Rank + "
\n";
        
        res.send(metricsString);
    }
})

app.listen(3000, function() {
    console.log("Test express server listening on port 3000");
});