51Degrees Device Detection for Node.js

cloud/gettingstarted-console/gettingStarted.js

This example is available in full on GitHub.

This example is available in full on GitHub.Required npm Dependencies:

/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../node_modules/', requestedPackage));
} catch (e) {
return require(path.join(__dirname, '/../../../../', requestedPackage));
}
};
const fs = require('fs');
const PipelineBuilder = require51('fiftyone.pipeline.core').PipelineBuilder;
const ExampleUtils = require(path.join(__dirname, '/../exampleUtils'));
const exampleConstants = require51('fiftyone.devicedetection.shared').exampleConstants;
const OptionsExtension = require51('fiftyone.devicedetection.shared').optionsExtension;
const DataExtension = require51('fiftyone.devicedetection.shared').dataExtension;
const outputValue = function (name, value) {
// Individual result values have a wrapper called
// `AspectPropertyValue`. This functions similarly to
// a null-able type.
// If the value has not been set then trying to access the
// `value` property will throw an exception.
// `AspectPropertyValue` also includes the `noValueMessage`
// property, which describes why the value has not been set.
return `\n\t${name}: ${value}`;
};
const analyse = async function (evidence, pipeline, output) {
// FlowData is a data structure that is used to convey
// information required for detection and the results of the
// detection through the pipeline.
// Information required for detection is called "evidence"
// and usually consists of a number of HTTP Header field
// values, in this case represented by a
// Object of header name/value entries.
// list the evidence
let message = 'Input values:';
for (const [key, value] of evidence) {
message += `\n\t${key}: ${value}`;
}
output.write(message + '\n');
const data = pipeline.createFlowData();
// Add the evidence values to the flow data
evidence.forEach((value, key, map) => {
data.evidence.add(key, value);
});
await data.process();
message = 'Results:';
// Now that it's been processed, the flow data will have
// been populated with the result. In this case, we want
// information about the device, which we can get by
// asking for the 'device' data.
const device = data.device;
// Track whether any property is left without a value, which is
// usually because the resource key does not include the property.
let anyValueMissing = false;
const getValue = function (propertyName) {
try {
const property = device[propertyName];
if (!property || property.hasValue !== true) {
anyValueMissing = true;
}
} catch (e) {
anyValueMissing = true;
}
return DataExtension.getValueHelper(device, propertyName);
};
// Display the results of the detection, which are called
// device properties. See the property dictionary at
// /developers/property-dictionary?utm_source=code&utm_medium=example&utm_campaign=device-detection-node&utm_content=fiftyone.devicedetection.cloud-examples-cloud-gettingstarted-console-gettingstarted.js&utm_term=analyse
// for details of all available properties.
message += outputValue('Mobile Device', getValue('ismobile'));
message += outputValue('Platform Name', getValue('platformname'));
message += outputValue('Platform Version', getValue('platformversion'));
message += outputValue('Browser Name', getValue('browsername'));
message += outputValue('Browser Version', getValue('browserversion'));
message += '\n\n';
output.write(message);
return anyValueMissing;
};
const run = async function (options, output) {
const resourceKey = OptionsExtension.getResourceKey(options);
// If we don't have a resource key then log an error
if (!resourceKey) {
console.log(
'No resource key specified in the configuration file ' +
'\'51d.json\' or the environment variable ' +
`'${ExampleUtils.RESOURCE_KEY_ENV_VAR}'. The 51Degrees cloud ` +
'service is accessed using a \'ResourceKey\'. For more information ' +
'see ' +
'/documentation/_info__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=device-detection-node&utm_content=fiftyone.devicedetection.cloud-examples-cloud-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required. ' +
'A resource key with the free properties used by this example can ' +
'be created at https://configure.51degrees.com/Wkqxf3Bs?utm_source=code&utm_medium=example&utm_campaign=device-detection-node&utm_content=fiftyone.devicedetection.cloud-examples-cloud-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required. A free ' +
'key populates the free properties only, whilst a key created at ' +
'https://configure.51degrees.com/hYzn3TV3?utm_source=code&utm_medium=example&utm_campaign=device-detection-node&utm_content=fiftyone.devicedetection.cloud-examples-cloud-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required also includes the paid ' +
'properties this example displays. See ' +
'/pricing?utm_source=code&utm_medium=example&utm_campaign=device-detection-node&utm_content=fiftyone.devicedetection.cloud-examples-cloud-gettingstarted-console-gettingstarted.js&utm_term=resource-key-required to get a paid subscription with ' +
'more properties. Once complete, populate the config file or ' +
'environment variable mentioned at the start of this message ' +
'with the key.'
);
return;
}
const pipeline = new PipelineBuilder().buildFromConfiguration(options);
// To monitor the pipeline we can put in listeners for various log events.
// Valid types are info, debug, warn, error
pipeline.on('error', console.error);
// carry out some sample detections
let anyValueMissing = false;
for (const values of exampleConstants.defaultEvidenceValues) {
if (await analyse(values, pipeline, output)) {
anyValueMissing = true;
}
}
// If any property was left without a value then show the common
// pricing message once after the results.
if (anyValueMissing) {
output.write(ExampleUtils.PRICING_MESSAGE + '\n');
}
};
// Don't run the server if under TEST
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
// Use the supplied resource key or try to obtain one
// from the environment variables.
const resourceKey = args.length > 0 ? args[0] : ExampleUtils.getResourceKeyFromEnv();
// Load the configuration from a config file to a JSON object.
const options = JSON.parse(fs.readFileSync('51d.json'), 'utf8');
const resourceKeyFromConfig = OptionsExtension.getResourceKey(options);
if (!resourceKeyFromConfig || resourceKeyFromConfig.startsWith('!!')) {
OptionsExtension.setResourceKey(options, resourceKey);
}
run(options, process.stdout);
}
module.exports = {
run
};
Definition dataExtension.js:23
static getValueHelper(elementData, propertyName)
Helper function to read property values from flowData.
Definition dataExtension.js:31
Definition fiftyone.devicedetection.onpremise/examples/onpremise/exampleUtils.js:41
Definition optionsExtension.js:27
static setResourceKey(options, resourceKey)
Set resource key.
Definition optionsExtension.js:120
static getResourceKey(options)
Get resource Key.
Definition optionsExtension.js:105