Enigma.js Application – Server setup – NodeJS and ExpressJS

codewander.com-enigma.js-example-noddejs-server-setup

This post is part of the Enigma.js example series. Prior to the server setup, please complete the Enigma.js Applicaiton-ReactJS-Client-Setup.

It assumes that the node JS is already installed. If not, please download the nodejs. I will use windows 10 OS.

Set up folder

Create a folder server in the main app folder.

07.codewander.com-enigmajs-app-server-folder

 

Install dependencies

For now, we will install the following dependencies. Since we have no database need at this point, we do not install any. But incase a need arises, we will use mongoDB with mongoose.

  1. cors
  2. express

Go to the command prompt and get to the app folder and run the following command

 npm i cors express¬†

07.server dependency install

Once the node JS dependedncy installed, run the following command to install the enigma.js and ws (websocket) modules.

 npm i -S enigma.js ws


07.server dependency install-enigma-ws

Set up Server

The code in server is typically split into

  1. Controllers
  2. Models
  3. Routes

Since, we do not have any need to persist or retrieve any entity from database, we start with setting up of Controllers and Routes at this point. However, we create the following folder structure inside the server

Set up Controllers

We create one controller to act on each components in qlik sense environment. To begin with, wee create a controller for the engine level data and call it qengine.ctrl.js and place the following code. We will get into the details of enigma.js later in the series. But for now, the focus is in getting basic code structure for the server module.

module.exports = {
/**
* Get All Apps
*/

getApps: (req, res, next) => {

res.json({msg:'Gets all the apps from the qlik server'}));

}

}

Set up Routes

Similar to the controller, I create one route file that handles requests on each component of qlik sense. To begin with, I create a route file, qengine.js under routes folder and place the following code inside

const enginecontroller = require('./../controllers/qengine.ctrl')

module.exports = (router) => {
/**
* get All Apps
*/

router
.route('/apps')
.get(enginecontroller.getApps)

}

Create Express JS instance to bundle all routes

Create a file, index.js under routes folder and place the below code

const qengine = require('./qengine')

module.exports = (router) => {
app(router)
qengine(router)
}

Create server entry point

Create a file app.js under the server folder and place the below code

/** require dependencies */
const express = require("express")
const routes = require('./routes/')
const cors = require('cors')

const app = express()
const router = express.Router()

let port = 5000 || process.env.PORT

/** set up routes {API Endpoints} */
routes(router)

/** set up middlewares */
app.use(cors())

app.use('/api', router)

/** start server */
app.listen(port, () => {
console.log('Server started at port: ${port}');
});

Test the server setup

In the command prompt, go to the server folder and run the below command

node app.js

You get the below output which means the server has started successful in the given port. Now access the below url in the browser

http://localhost:5000/api/apps

You must get the below output, which means that the server structure is complete and we are good to advance into creating working enigma.js application using Node js and React js.

09.codewandere.com-enigmajs-app-server-testing-output

Our folder structure looks like below.

09.1.codewandere.com-enigmajs-app-server-folder-structure

 

1 Comment

  1. – app(router) in routers does not exist
    – on all places arrow function => is replaced by =>
    – instead, backticks here are normal single quotes console.log(‘Server started at port: ${port}’) so you can’t see the port

Leave a Reply

Your email address will not be published.


*