This example shows how to use 51Degrees on-premise device detection to determine details about a
device based on its User-Agent and User-Agent Client Hint HTTP header values.
This example requires a local data file. The free 'Lite' data file can be acquired by
pulling the git submodules under this repository (run `git submodule update --recursive`)
or from the device-detection-data
GitHub repository.
The Lite data file is only used for illustration, and has limited accuracy and capabilities.
Find out about the more capable data files that are available on our
pricing page
const require51 = (requestedPackage) => {
try {
return require('/../' + requestedPackage);
} catch (e) {
return require(requestedPackage);
}
};
const fs = require('fs');
const http = require('http');
const path = require('path');
const pug = require('pug');
const compiledFunction =
pug.compileFile(path.join(__dirname, '/index.pug'));
const core = require51('fiftyone.pipeline.core');
const optionsExtension =
require('fiftyone.devicedetection.shared').optionsExtension;
const dataExtension =
require('fiftyone.devicedetection.shared').dataExtension;
require(path.join(__dirname, '/../exampleUtils'));
var pipeline;
const setPipeline = (options) => {
const dataFilePath = optionsExtension.getDataFilePath(options);
if (!dataFilePath) {
throw 'A data file must be specified in the 51d.json file.';
}
if (!fs.existsSync(dataFilePath)) {
const newDataFilePath =
ExampleUtils.
findFile(path.basename(dataFilePath));
if (newDataFilePath !== undefined) {
optionsExtension.setDataFilePath(options, newDataFilePath);
console.warn('Failed to find a device detection data file at ' +
`at the specified path '${dataFilePath}'. Use '${newDataFilePath}' ` +
'instead.');
} else {
throw 'Failed to find a device detection data file at ' +
`'${dataFilePath}'. If using the lite file, then make sure the ` +
'device-detection-data submodule has been updated by running ' +
'\'git submodule update --recursive\'. Otherwise, ensure that the ' +
'filename is correct in 51d.json.';
}
}
pipeline = new core.PipelineBuilder({
addJavaScriptBuilder: true,
javascriptBuilderSettings: {
endPoint: '/json'
}
}).buildFromConfiguration(options);
pipeline.on('error', console.error);
};
const server = http.createServer((req, res) => {
const flowData = pipeline.createFlowData();
flowData.evidence.addFromRequest(req);
if (req.url.startsWith('/json')) {
flowData.process().then(function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(flowData.jsonbundler.json));
});
} else {
flowData.process().then(function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
core.Helpers.setResponseHeaders(res, flowData);
const allEvidence = flowData.evidence.getAll();
const evidences =
pipeline.getElement('device').evidenceKeyFilter.filterEvidence(
allEvidence);
res.end(compiledFunction(
{
dataFilePublishedTime: new Date().getTime(),
dataFileAgeWarning: DATA_FILE_AGE_WARNING,
responseHeaders: res.getHeaders(),
evidenceUsed: evidences,
allEvidence: allEvidence,
fiftyOneJs: flowData.javascriptbuilder.javascript,
hardwareVendor: dataExtension.getValueHelper(flowData.device, 'hardwarevendor'),
hardwareName: dataExtension.getValueHelper(flowData.device, 'hardwarename'),
deviceType: dataExtension.getValueHelper(flowData.device, 'devicetype'),
platformVendor: dataExtension.getValueHelper(flowData.device, 'platformvendor'),
platformName: dataExtension.getValueHelper(flowData.device, 'platformname'),
platformVersion: dataExtension.getValueHelper(flowData.device, 'platformversion'),
browserVendor: dataExtension.getValueHelper(flowData.device, 'browservendor'),
browserName: dataExtension.getValueHelper(flowData.device, 'browsername'),
browserVersion: dataExtension.getValueHelper(flowData.device, 'browserversion'),
screenWidth: dataExtension.getValueHelper(flowData.device, 'screenpixelswidth'),
screenHeight: dataExtension.getValueHelper(flowData.device, 'screenpixelsheight')
})
);
});
}
});
if (process.env.JEST_WORKER_ID === undefined) {
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')));
setPipeline(options);
const port = 3001;
server.listen(port);
console.log('Server listening on port: ' + port);
};
module.exports = {
server: server,
setPipeline: setPipeline
};