In this article, we describe how to write the node.js program in Ubuntu. Here we create a simple application that returns “Hello World”. You can create your own application. Here we provide some steps for that.
- Create and edit your Node.js application. Here, we will edit a sample application called helloworld.js.
cd ~
vim helloworld.js
- Insert the code below into your the helloworld.js file.
dvar http = require('http');
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'APP_SERVER_PUBLIC_IP');
console.log('Server running at http://APP_SERVER_PUBLIC_IP_ADDRESS:8080/');
You can replace the port, 8080, in both locations (be sure to use a non-admin port, i.e. 1024 or greater).
- Save and exit.
This Node.js application will listen on the specified port and IP addresses and return “Hello World” with a 200 HTTP success code.
Let’s Test the Application
- Test your application on your app server:
node helloworld.js
Note: Terminate the application by pressing CTRL+C on your app server.
- Test your application in a browser window, using your own public IP.
http://YOUR_APP_SERVER_IP:8080
- Have you received the “Hello World” output? your application is working properly.
And if you get an incorrect output, make sure your Node.js application is running and configured to listen on the correct IP address and port.
Note: Terminate the application by pressing CTRL+C on your app server.
Set your Node.js application to use your Private IP address.
Here we check that the application is working properly, now we can move the helloworld.js IPs from the app server’s public IP to the private IP. Your App server’s Private IP address can be found in Cloud Servers under the IP & DNS tab. Find a server’s private IP address.
Again open helloworld.js file with vim editor.
cd ~
vim hello.js
Here we have to replace the APP_SERVER_PRIVATE_IP:8080 with your app server’s Private IP address.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'APP_SERVER_PRIVATE_IP');
console.log('Server running at http://APP_SERVER_PRIVATE_IP_ADDRESS:8080/');
Save and exit.
Install and configure PM2
PM2 is used to manage applications. It is a process manager for Node.js applications. npm (node js package manager) is used to install the PM2.
sudo npm install -g pm2
Note: -g is used to install PM2 as globally.
Manage Application with PM2
- We can start the application using pm2, it runs in the background.
pm2 start helloworld.js
When you start your application PM2 automatically assigns an App name. Here App name is your file name without .js extension. As shown below, PM2 maintain the process id pid, application status, memory occupied by the application.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ cpu │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼────────┤
│helloworld│ 0 │ fork │ 3524 │ online │ 0 │ 0s │ 21.566 MB │ 35% │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴────────┘
Some times application crashes or killed, at that time, using startup sub-command, PM2 will restart the application automatically.
pm2 startup systemd
Other PM2 Usage (Optional)
PM2 provides many subcommands that used to manage the information of applications.
PM2 Sub-Commands | Description |
$ pm2 stop App or ID | Stop an application |
$ pm2 restart App or ID | Restart the application |
$ pm2 list | List the applications currently managed by PM2 |
$ pm2 info App or ID | Find more information about a specific application |
$ pm2 monit | Show memory usage, CPU usage, and application status |
Set up the Reverse Proxy Server with NGINX
After creating and testing the application, it’s time to allow access to the users. we will set up an NGINX web server as a reverse proxy, which will allow accessing our application to the users.
- Update your packages:
sudo apt-get update
- Install NGINX:
sudo apt-get install nginx
- Open the NGINX server block configuration file:
sudo vim /etc/nginx/sites-available/default
- Add the server’s Private IP address for the APP_SERVER_PRIVATE_IP portion. Change the port (8080) if you set up your application is set to listen on a different port.
- NGINX location block before edit:
location / {
}
- NGINX location block after edit:
location / {
proxy_pass http://APP_SERVER_PRIVATE_IP:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Note: Using this configuration allows the Web Server to respond to requests at its root.
- Restart NGINX on your Web server:
sudo service nginx restart
Now Node.js application is running, you can access your application through the reverse proxy of the web server. You can test it by accessing your web server’s URL.
Have you seen Hello World message in your browser? Your installation and configuration were successful.
Now you can create your own application with node js.
See More: JavaScript Framework
The post How to write node .js program in ubuntu appeared first on I'm Programmer.