Node.js is an open-source JavaScript runtime environment for easily building server-side and networking applications. The platform runs on Linux, OS X, FreeBSD, and Windows. Node.js applications can be run at the command line, but we’ll focus on running them as a service to restart on reboot or failure automatically. They can safely be used in a production environment.

In this article, it will become clear on how to set up Node JS for production.

Prerequisites

This guide assumes that you have the following:

  • An Ubuntu 20.04 server.
  • A domain name pointed at your server’s public IP.
  • Nginx installed.
  • Nginx configured with SSL using Let’s Encrypt certificates.

Install Node JS

We will install the latest LTS release of Node.js.

$ cd ~
$ curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh

Then run the script:

sudo bash nodesource_setup.sh

After running the setup script from node source, you can install the Node.js package in the same way that you did above:

$ sudo apt-get install nodejs

To be sure that all the packages will work you need to install ‘build-essentials’:

$ sudo apt-get install build-essential

Creating a Node JS application

First, create and open your Node.js application for editing. If you are new to Node JS and want to create smarter applications, you can check my Guide for Node JS — Express Framework.

$ cd ~
$ nano app.js

Insert the following lines of code:

const http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Medium\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

This is a very simple Hello ‘Medium’ program. When you exit the program and save it, we are going to test your application.

#programming #javascript #networking #technology #linux

How to Deploy Node JS to Ubuntu 20.04 in 2020.
7.95 GEEK