This example shows how to use the 51Degrees Cloud service to determine details about a
device based on its User-Agent and User-Agent Client Hint HTTP header values.
const require51 = (requestedPackage) => {
try {
return require('/../' + requestedPackage);
} catch (e) {
return require(requestedPackage);
}
};
const path = require('path');
const fs = require('fs');
const http = require('http');
const pug = require('pug');
const compiledFunction = pug.compileFile(path.join(__dirname, '/index.pug'));
const core = require51('fiftyone.pipeline.core');
require('fiftyone.devicedetection.shared').optionsExtension;
require('fiftyone.devicedetection.shared').dataExtension;
const ExampleUtils = require(path.join(__dirname,
'/../exampleUtils'));
var pipeline;
const setPipeline = (options) => {
if (!resourceKey) {
throw '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 ' +
'https://51degrees.com/documentation/_info__resource_keys.html. ' +
'A resource key with the properties required by this example can be ' +
'created for free at https://configure.51degrees.com/1QWJwHxl. ' +
'Once complete, populate the config file or environment variable ' +
'mentioned at the start of this message with the key.';
}
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('cloud').evidenceKeyFilter.filterEvidence(
allEvidence);
const device = flowData.device;
res.end(compiledFunction(
{
responseHeaders: res.getHeaders(),
evidenceUsed: evidences,
allEvidence: allEvidence,
fiftyOneJs: flowData.javascriptbuilder.javascript,
hardwareVendor:
DataExtension.
getValueHelper(device,
'hardwarevendor'),
hardwareName:
DataExtension.getValueHelper(device,
'hardwarename'),
platformVendor:
DataExtension.getValueHelper(device,
'platformvendor'),
platformName:
DataExtension.getValueHelper(device,
'platformname'),
platformVersion:
DataExtension.getValueHelper(device,
'platformversion'),
browserVendor:
DataExtension.getValueHelper(device,
'browservendor'),
browserName:
DataExtension.getValueHelper(device,
'browsername'),
browserVersion:
DataExtension.getValueHelper(device,
'browserversion'),
screenWidth:
DataExtension.getValueHelper(device,
'screenpixelswidth'),
screenHeight:
DataExtension.getValueHelper(device,
'screenpixelsheight')
})
);
});
}
});
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
const resourceKey =
args.length > 0 ? args[0] : process.env[
ExampleUtils.RESOURCE_KEY_ENV_VAR];
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')));
if (!resourceKeyFromConfig || resourceKeyFromConfig.startsWith('!!')) {
}
setPipeline(options);
const port = 3001;
server.listen(port);
console.log('Server listening on port: ' + port);
};
module.exports = {
server: server,
setPipeline: setPipeline
};