How to Run a NodeJS Service in Linux CentOS 8 virtual server

Posted on

Introduction

This article will focus on how to run a NodeJS service in Linux CentOS 8 virtual server. Actually, before running a NodeJS service in a Linux CentOS 8 virtual server or any server running with any operating system, just install the NodeJS first. So, installing the NodeJS is important. Check the article with the title of ‘How to Install NodeJS in Linux CentOS 8 virtual server’ in this link. So, first of all just install the NodeJS in Linux CentOS 8 virtual server.

Running NodeJS Service in Linux CentOS 8 virtual server

The following sequence to run a NodeJS service :

1. As usual, access the virtual server. Either directly access the virtual server or remotely access it. A remote access method available for an example is SSH remote connection. Just read for a reference in the article with the title of ‘How to Remote CentOS Virtual Server running in a VirtualBox with a NAT Network using SSH’ in this link.

2. After successfully logging in to the virtual server, do not forget to check whether NodeJS command line is available. Check by executing the following command :

node -v
npm -v

The following is the execution of the above command :

[root@10 nodejs]# node -v
v14.15.4
[root@10 nodejs]# npm -v
6.14.10
[root@10 nodejs]#

3. Following after, create a new file with the following content :

var http = require('http');
var server = http.createServer(function (req, res) {
          res.end('Hello World !');
});
server.listen(3001, "0.0.0.0");
console.log('Server running at http://0.0.0.0:3001/');

The above script is hinting on create a new service listening on available network interface in port 3001. The line informing that condition is in one of the line below :

server.listen(3001, "0.0.0.0");

Every time accessing the address with the specified port using web browser, it will display the string ‘Hello World !’. So, it is not appear in the command line interface as in the following script :

console.debug('Hello World !');

As it appear in the article with the title of ‘How to Run Simple HelloWorld NodeJS script in Linux CentOS virtual server’ in this link. On the other hand, it will appear in the web browser as follows :

How to Run a NodeJS Service in Linux CentOS 8 virtual server

3. In order to be able to preview the output on the web browser, after create a new NodeJS script as in the third step, run it. Just execute the following command in order to be able to run it :

node js_file_name.js

Since the file name of the NodeJS script is ‘demo_server.js’, just run it as follows :

[root@10 nodejs]# node demo_server.js
Server running at http://0.0.0.0:3001/

4. So, how can host machine access the above address where the actual service is running in the guest machine ?. In other words, the service is running locally in the virtual server. How can a host machine access it from the outside into the inside of guest machine ? . The answer is by adding forwarding rule so that the access from the host machine is possible to the guest machine or to the virtual server. Read the article with the title of ‘How to Add Port Forwarding Rule to access NodeJS Service of Guest Machine running in VirtualBox Manager from Host Machine’ in this link to know how to be able to do it.

Leave a Reply