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 http = require('http');
const pug = require('pug');
const compiledFunction = pug.compileFile(path.join(__dirname, '/index.pug'));
const publicDir = path.join(__dirname, '/public');
const staticContentTypes = {
'.css': 'text/css',
'.js': 'text/javascript'
};
const tryServeStatic = (req, res) => {
const urlPath = req.url.split('?')[0];
if (!urlPath.startsWith('/css/') && !urlPath.startsWith('/js/')) {
return false;
}
const filePath = path.join(publicDir, urlPath);
if (!filePath.startsWith(publicDir)) {
res.statusCode = 403;
res.end();
return true;
}
fs.readFile(filePath, function (err, content) {
if (err) {
res.statusCode = 404;
res.end();
return;
}
res.statusCode = 200;
res.setHeader('Content-Type',
staticContentTypes[path.extname(filePath)] || 'application/octet-stream');
res.end(content);
});
return true;
};
const core = require51('fiftyone.pipeline.core');
require51('fiftyone.devicedetection.shared').optionsExtension;
require51('fiftyone.devicedetection.shared').dataExtension;
const ExampleUtils = require(path.join(__dirname,
'/../exampleUtils'));
let 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 ' +
'/documentation/_info__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=device-detection-node&utm_content=fiftyone.devicedetection.cloud-examples-cloud-gettingstarted-web-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-web-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-web-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-web-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.';
}
pipeline = new core.PipelineBuilder({
addJavaScriptBuilder: true,
javascriptBuilderSettings: {
endPoint: '/json'
}
}).buildFromConfiguration(options);
pipeline.on('error', console.error);
};
const server = http.createServer((req, res) => {
if (tryServeStatic(req, res)) {
return;
}
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,
fiftyOneJs: flowData.javascriptbuilder.javascript,
})
);
});
}
});
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
const resourceKey =
args.length > 0 ? args[0] :
ExampleUtils.getResourceKeyFromEnv();
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')));
if (!resourceKeyFromConfig || resourceKeyFromConfig.startsWith('!!')) {
}
setPipeline(options);
const port = process.env.PORT || 3001;
const hostname = 'localhost';
server.listen(port, hostname);
console.log(`Server listening on: http:
}
module.exports = {
server,
setPipeline
};
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