This example demonstrates how to enhance results by incorporating evidence
from client-side JavaScript.
This approach is required to perform certain tasks such as identify iPhone
and iPad models or get the screen resolution for a desktop device.
Properties containing JavaScript snippets are populated by engines in the
Pipeline and these are then bundled together into a single JavaScript
block by the 'JsonBuilder' and 'JavaScriptBuilder' elements.
This JavaScript is then used to obtain the additional evidence from the
client and pass it back to the server. The updated results can then be
used immediately on the client-side or on subsequent requests to the server.
This example requires a local data file. Free data files can be acquired by
pulling the submodules under this repository or from the
device-detection-data
GitHub repository.
const FiftyOneDegreesDeviceDetection = require((process.env.directory || __dirname) + '/../../');
const fs = require('fs');
const datafile = (process.env.directory || __dirname) + '/../../device-detection-cxx/device-detection-data/51Degrees-LiteV4.1.hash';
if (!fs.existsSync(datafile)) {
console.error("The datafile required by this example is not present. Please ensure that the 'device-detection-data' submodule has been fetched.");
throw ("No data file at '" + datafile + "'");
}
const pipeline = new FiftyOneDegreesDeviceDetection.DeviceDetectionPipelineBuilder({
performanceProfile: 'MaxPerformance',
dataFile: datafile,
autoUpdate: false,
javascriptBuilderSettings: {
endPoint: '/json'
}
}).build();
pipeline.on('error', console.error);
const http = require('http');
const server = http.createServer((req, res) => {
const flowData = pipeline.createFlowData();
flowData.evidence.addFromRequest(req);
flowData.process().then(function () {
res.statusCode = 200;
if (req.url.startsWith('/json')) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(flowData.jsonbundler.json));
} else {
res.setHeader('Content-Type', 'text/html');
let output = '';
if (flowData.device.hardwarename && flowData.device.hardwarename.hasValue) {
console.log("Client's updated hardware name is " + flowData.device.hardwarename.value);
}
if (flowData.device.screenpixelswidth.hasValue && flowData.device.screenpixelswidth.value) {
console.log("Client's screen pixel width is " + flowData.device.screenpixelswidth.value);
}
output += `
<p id=hardwarename></p>
<p id=screenpixelwidth></p>
<script>
window.onload = function(){
fod.complete(function (data) {
if(data.device["hardwarename"]){
document.getElementById('hardwarename').innerHTML = "<strong>Updated Hardware Name from client-side evidence:</strong> " + data.device["hardwarename"];
}
document.getElementById('screenpixelwidth').innerHTML = "<strong>Pixel width: " + data.device.screenpixelswidth + "</strong>"
});
}
</script>
`;
const js = `<script>${flowData.javascriptbuilder.javascript}</script>`;
res.end(js + output);
}
});
});
const port = 3001;
server.listen(port);
console.log('Server listening on port: ' + port);