This example demonstrates using both device detection and IP intelligence from the 51Degrees cloud service as part of a simple web server.
This example demonstrates using both device detection and IP intelligence from the 51Degrees cloud service as part of a simple web server. The User-Agent and IP address of the requesting client are used as evidence and details about the device, network, and location are displayed.
The page also loads the 51Degrees client-side script from '/51Degrees.core.js'. The script gathers evidence in the browser, such as the screen size, and posts that evidence to '/json', which answers with the refined results.
To run this example, you will eventually need to create a resource key. A resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties, but it is not yet available for IP intelligence. For now, you can run the onpremise/mixed/gettingstarted-api example to simulate a custom hosted cloud service, and point this example to it by setting the '_51DEGREES_CLOUD_ENDPOINT' environment variable to 'http://localhost:5225/'. Any value can then be used for the resource key.
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../../node_modules/', requestedPackage));
} catch (e) {
try {
return require(requestedPackage);
} catch (e2) {
return require(requestedPackage);
}
}
};
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');
const optionsExtension =
require51('fiftyone.ipintelligence.shared').optionsExtension;
const dataExtension =
require51('fiftyone.ipintelligence.shared').dataExtension;
const ExampleUtils = require(path.join(__dirname,
'/../../exampleUtils'));
const coreJsPath = '/51Degrees.core.js';
const jsonPath = '/json';
let pipeline;
const setPipeline = (options) => {
const resourceKey = optionsExtension.getResourceKey(options);
if (!resourceKey || resourceKey.startsWith('!!')) {
throw 'No resource key specified in the configuration file ' +
'\'51d.json\' or the environment variable ' +
`'${ExampleUtils.RESOURCE_KEY_VARIABLE}'. The 51Degrees cloud ` +
'service is accessed using a \'ResourceKey\'. For more information ' +
'see ' +
'/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-web-gettingstarted.js&utm_term=resource-key-required. ' +
'A resource key with everything this example displays can be ' +
'created at https://configure.51degrees.com/hYzn3TV3?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-web-gettingstarted.js&utm_term=resource-key-required with a paid ' +
'subscription. A free key created at ' +
'https://configure.51degrees.com/Wkqxf3Bs?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-gettingstarted-web-gettingstarted.js&utm_term=resource-key-required populates only the ' +
'free properties, which for IP intelligence are Country, ' +
'LocationConfidence, Ip and IpV6. See ' +
'/pricing?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-mixed-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: jsonPath
}
}).buildFromConfiguration(options);
pipeline.on('error', console.error);
};
const publicDir = path.join(__dirname, '/public');
const server = http.createServer(async (req, res) => {
return;
}
const flowData = pipeline.createFlowData();
try {
await flowData.evidence.addFromRequestAsync(req);
} catch (e) {
res.statusCode = 400;
res.end();
return;
}
const urlPath = req.url.split('?')[0];
if (urlPath === jsonPath) {
flowData.process().then(function () {
core.Helpers.writeClientSideResponse(req, res, flowData,
'application/json', JSON.stringify(flowData.jsonbundler.json));
});
return;
}
if (urlPath === coreJsPath) {
flowData.process().then(function () {
core.Helpers.writeClientSideResponse(req, res, flowData,
'application/x-javascript', flowData.javascriptbuilder.javascript);
});
return;
}
flowData.process().then(function () {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
const allEvidence = flowData.evidence.getAll();
const evidences =
pipeline.getElement('cloud').evidenceKeyFilter.filterEvidence(
allEvidence);
const inputIpAddress =
(allEvidence['query.client-ip'] ||
allEvidence['header.x-forwarded-for'] || '');
res.end(compiledFunction(
{
evidenceUsed: evidences,
allEvidence,
inputIpAddress,
coreJsPath,
areas: dataExtension.getValueHelper(flowData.ip, 'areas'),
isMobile: dataExtension.getValueHelper(flowData.device, 'ismobile'),
platformName: dataExtension.getValueHelper(flowData.device, 'platformname'),
platformVersion: dataExtension.getValueHelper(flowData.device, 'platformversion'),
browserName: dataExtension.getValueHelper(flowData.device, 'browsername'),
browserVersion: dataExtension.getValueHelper(flowData.device, 'browserversion'),
deviceType: dataExtension.getValueHelper(flowData.device, 'devicetype'),
deviceId: dataExtension.getValueHelper(flowData.device, 'deviceid'),
registeredName: dataExtension.getValueHelper(flowData.ip, 'registeredname'),
registeredOwner: dataExtension.getValueHelper(flowData.ip, 'registeredowner'),
registeredCountry: dataExtension.getValueHelper(flowData.ip, 'registeredcountry'),
ipRangeStart: dataExtension.getValueHelper(flowData.ip, 'iprangestart'),
ipRangeEnd: dataExtension.getValueHelper(flowData.ip, 'iprangeend'),
country: dataExtension.getValueHelper(flowData.ip, 'country'),
countryCode: dataExtension.getValueHelper(flowData.ip, 'countrycode'),
town: dataExtension.getValueHelper(flowData.ip, 'town'),
latitude: dataExtension.getValueHelper(flowData.ip, 'latitude'),
longitude: dataExtension.getValueHelper(flowData.ip, 'longitude')
})
);
});
});
if (process.env.JEST_WORKER_ID === undefined) {
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')));
const resourceKeyFromConfig = optionsExtension.getResourceKey(options);
if (!resourceKeyFromConfig || resourceKeyFromConfig.startsWith('!!')) {
optionsExtension.setResourceKey(
}
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 fiftyone.ipintelligence.onpremise/examples/onpremise/exampleUtils.js:203