Setting up the Analytics SDK Server with Node.js

    Step 1 - Create the Node.js Project

    1 - Open a command line and create a directory named reveal-server-node

    > mkdir reveal-server-node
    

    2 - Change the command line path to the newly created directory

    > cd reveal-server-node
    

    3 - Initialize npm in the directory

    > npm init -y
    

    4 - Install the express framework

    > npm install express
    

    5 - Open the project in VS Code

    > code .
    

    6 - Create a new file named main.js, and intialize add the following code

    var express = require('express');
    
    const app = express();
    
    app.listen(8080, () => {
        console.log(`Analytics server accepting http requests`);
    });
    

    Step 2 - Add Analytics SDK

    1 - Install the Analytics SDK for Node.js

    > npm install reveal-sdk-node
    

    2 - Modify the main.js file to add Analytics

    var express = require('express');
    var reveal = require('reveal-sdk-node');
    
    const app = express();
    
    //add reveal sdk
    app.use('/', reveal());
    
    app.listen(8080, () => {
        console.log(`Analytics server accepting http requests`);
    });
    

    Step 3 - Create the Dashboards Folder

    1 - In Visual STudio Code, click the New Folder button in the Explorer and name it dashboards. The folder MUST be named dashboards

    By default, the Analytics SDK uses a convention that will load all dashboards from the dashboards folder. You can change this convention by creating a custom IRVDashboardProvider.

    Step 4 - Setup CORS Policy (Debugging)

    While developing and debugging your application, it is common to host the server and client app on different URLs. For example; your Server my be running on https://localhost:24519, while your Angular app may be running on https://localhost:4200. If you were to try and load a dashboard from the client application, it would fail because of a Cross-Origin Resource Sharing (CORS) policy. To enable this scenario, you must create a CORS policy and enable it in the server project.

    1 - Install cors package

    > npm install cors
    

    2 - Modify the main.js file to enable cors

    var express = require('express');
    var cors = require('cors');
    var reveal = require('reveal-sdk-node');
    
    const app = express();
    
    app.use(cors()); // DEVELOPMENT only! In production, configure appropriately.
    
    app.use('/', reveal());
    
    app.listen(8080, () => {
        console.log(`Analytics server accepting http requests`);
    });
    

    Step 5 - Start the Node.js Server

    The final step is to start the Node.js server by runnning the following command:

    > node main.js
    

    Next Steps:

    Note

    The source code to this sample can be found on GitHub.