Installing the Enforcer

Installing the Node.JS Express Enforcer

The Node.JS express Enforcer is used as a middleware on between the client and your node Express application.

In order to Install the Node.JS Express Enforcer you'll need to:

  1. Install the PerimeterX module via NPM
  2. Set the PerimeterX module as a middleware to you're express application.

Install the PerimeterX module via NPM:

npm install --save perimeterx-node-express

The final step is to set PerimeterX module as a middleware to your express application endpoints.
For that you will first need to get the required configurations described below.

📘

Note:

For more personal customization of the PerimeterX Enforce to fit your application need, and to enable advanced Enforcer features, visit the Configuration Options page.

Required configurations

  • The PerimeterX Application ID / AppId and PerimeterX Token / Auth Token can be found in the Portal, under Applications section.

  • The PerimeterX Cookie Encryption Key can be found in the portal, under Policies section.

The Policy from where the Cookie Encryption Key is taken must correspond with the Application from where the Application ID / AppId and PerimeterX Token / Auth Token

Require the PerimeterX node express module :

const perimeterx = require('perimeterx-node-express');

Initiate a pxConfig object contains px_app_id - Your Application ID, px_cookie_secret - Your Cookie Encryption Key, px_auth_token - Auth Token

const pxConfig = {
    px_app_id: 'PX_APP_ID',
    px_cookie_secret: 'PX_COOKIE_SECRET',
    px_auth_token: 'PX_AUTH_TOKEN'
};

Initialize PerimeterX's Enforcer:

perimeterx.init(pxConfig);

Set PerimeterX as a middleware to your application:

/*  block users with high bot scores using px-module for the route /helloWorld */
server.get('/helloWorld', perimeterx.middleware, (req, res) => {
    res.send('Hello from PX');
});

Setting the PerimeterX middleware to a specific endpoint

The following example demonstrate how to set the PerimeterX module as a middleware to a specific endpoint.

"use strict";

const express = require('express');
const perimeterx = require('perimeterx-node-express');

const server = express();

/* px-module and cookie parser need to be initiated before any route usage */
const pxConfig = {
    px_app_id: 'PX_APP_ID',
    px_cookie_secret: 'PX_COOKIE_SECRET',
    px_auth_token: 'PX_AUTH_TOKEN'
};
perimeterx.init(pxConfig);

/*  block users with high bot scores using px-module for the route /helloWorld */
server.get('/helloWorld', perimeterx.middleware, (req, res) => {
    res.send('Hello from PX');
});

server.listen(8081, () => {
    console.log('server started');
});

Setting the PerimeterX middleware on all server routes

"use strict";

const express = require('express');
const perimeterx = require('perimeterx-node-express');

const server = express();

/* the px-module and parser need to be initialized before any route usage */
const pxConfig = {
    px_app_id: 'PX_APP_ID',
    px_cookie_secret: 'PX_COOKIE_SECRET',
    px_auth_token: 'PX_AUTH_TOKEN'
};
perimeterx.init(pxConfig);

/* block high scored users using px-module for all routes */
server.use(perimeterx.middleware);

server.get('/helloWorld', (req, res) => {
    res.send('Hello from PX');
});

server.listen(8081, () => {
    console.log('server started');
});

Did this page help you?