Creating a backend NodeJS API

What if I said that you can create a back-end API endpoint to run some custom SQL query to get data and work upon. Plus this pulled data will have a response time in milli-seconds.

This received data can then be manipulated in the software of your choice. Some

examples are:

  • Excel
  • PowerBi
  • Front end web and mobile applications.
  • Python
  • Microprocessors. (Arduino, ESP..)

Before we start these are things that would need to be installed on your local machine.

If you have installed all the above. Let’s start.

You would need to download the below zip and extract the contents.
Zipcode

One the folder in VS Studio code and open .env file. This is your file that includes your server credentials. Fill out lines 2,3,4 and 5 and save.

Step

2. Click on “Terminal” tab and then “New Terminal”. In the terminal enter “npm install”. This will install the packages required to run the code. This might take minute or two to complete.

3. Now type “npm run” and it everything goes smooth you will “Server Running”.

Note: the default query pulls all the customer id’s from the Visual and displays them in json format. This query can be changed to anything you like.

Testing:
Open your web-browser and go to the below url:

  • http://localhost:5000/i. This will print at “OK” on the screen. If you see so that means the server is up and running.
  • http://localhost:5000/allCustomers.This will display all the customer id’s that are present in Visual.
  • Click on “Terminal” tab and then “New Terminal”. In the terminal enter “npm install”. This will install the packages required to run the code. This might take minute or two to complete.
  • Now type “npm run” and it everything goes smooth you will “Server Running”.

Congratulations, you successfully setup an server on your local machine.

However, this can only be accessed by your local machine and not by other people on the network. In order to deploy this over the whole local network, we will take some help from Pm2.

  • Open your terminal console and navigate to your code directory.
  • Type “PM2 start index.js” or in our case “pm2 start psi_reports_api.js”.

And the rest will be take care by Pm2.

Now you can access the same end on any computer over the network by just typing

http://”your server ip address”/allcustomers

Note: well this pm2 can also be setup on a dedicated server for more faster responses.

Leave a Comment