Setting up the Reveal 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
- Yarn
- pnpm
npm init -y
yarn init -y
pnpm init -y
4 - Install the express framework
- npm
- Yarn
- pnpm
npm install express
yarn add express
pnpm add express
5 - Open the project in VS Code
code .
6 - Create a new file named main.js, and add the following code
var express = require('express');
const app = express();
app.listen(8080, () => {
console.log(`Reveal server accepting http requests`);
});
Step 2 - Add Reveal SDK
1 - Install the Reveal SDK for Node.js
- npm
- Yarn
- pnpm
npm install reveal-sdk-node
yarn add reveal-sdk-node
pnpm add reveal-sdk-node
2 - Modify the main.js
file to add Reveal
var express = require('express');
var reveal = require('reveal-sdk-node');
const app = express();
app.use('/', reveal());
app.listen(8080, () => {
console.log(`Reveal 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 Reveal 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
- Yarn
- pnpm
npm install cors
yarn add cors
pnpm add 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(`Reveal 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
The source code to this sample can be found on GitHub.