NodeJS: development, server configuration

December 15, 2020

Lang: cs en de es

JavaScript is also gaining ground in the development of the backend of web applications to run on the server used technology NodeJS. How to start developing NodeJS applications? How to prepare a server for running NodeJS applications? Find out in this tutorial.

JavaScript is a scripting language that was primarily used on the frontend - it runs in the user's web browser. Eventually, technological developments unintentionally gave rise to the ability to run JavaScript on the backend - the NodeJS.

NodeJS installation

In addition to NodeJS, you will also need to install NPM, which is a package/library management tool that is developed in JavaScript. If you are using the Mageia distribution, you can install it with the following command.

urpmi nodejs npm
On debian-like distributions, to install NodeJS and NPM, use the following command:
apt-get install nodejs npm
You might be wondering what version of NodeJS you installed, so you can find out with the command:
node -v

Program code:

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;
console.log(process.env);

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Launching the application:
node hello.js
Once nodeJS is launched, the application is available in the web browser at: http://0.0.0.0:3000/ The source code for the example NodeJS application is on GitHub.

NodeJS Docker

The easiest way to get NodeJS working in Docker is to use the official Docker image NodeJS from docker hub - these NodeJS images can be used without modification. Here is a detailed guide to running NodeJS in a Docker container.

To try it out, you can run straight through my prepared NodeJS docker example.

Start the docker container and view the application:

docker-compose up
Manually build the container and manually start the application:
docker build -t nodex .
docker run -rm -p 49160:3000 nodex
The application will then be available at the url: http://localhost:49160/

NodeJS video tutorial

How to get NodeJS up and running? You can install NodeJS natively on your system or use Docker. Here's a video tutorial on how to set up your application development environment with NodeJS. And how to create your first program (hello world) in NodeJS.

NodeJS on server

After developing a NodeJS application, you will probably want to run it on the server as a daemon=still running program, that automatically starts when the server starts.
You can use various tools in a modern Linux-based OS to do this systemd. Another option is for example PM2, which is a process/application manager programmed in NodeJS.

PM2

PM2 works by ensuring that the NodeJS script runs in the background when the operating system starts and keeps it alive. It monitors the run and restarts it if it crashes for some reason. It automatically starts scripts on system boot and can run predefined scripts once.

To install, use the command:

npm install pm2@latest -g
Here is a detailed example of how to deploy PM2

Systemd

Here is a detailed example of how to run a NodeJS application using systemd.

Create a /lib/systemd/system/nodej-service.service file with the following configuration for running a NodeJS application:

[Unit]
Description=NodeJS
After=network.target

[Service]
Type=simple
#ExecStart=/usr/bin/node /srv/node/hello.js
ExecStart=/usr/bin/npm run start --prefix /root/CloudProxy
Restart=always
#Restart=on-failure

[Install]
WantedBy=multi-user.target
The following commands will reload the configuration and run the NodeJS application in the background(as a daemon):
systemctl daemon-reload
service nodejs-service start

Of course, it's good to do this configuration using some tool like ansible, because then you're able to automate these tasks and make them more efficient, which typically any good linux admin uses a tool like that. After all, if you want to use a NodeJS application for your business, you should delegate installing a Linux server to run NodeJS application to a server professional.

TypeScript

JavaScript is not a very good scripting language, despite the modernization, which is why today we often switch to TypeScript More about TypeScript on the official English site.

Articles on a similar topic

Analysis of assignment and pricing of software project development
Python program to control Docker using the API
How to use MailCatcher to test emails
Python OpenAI API
Creating a WebSocket web application and setting up a proxy
Project management: agile software development
How to run old PHP applications
What a good programmer should know
Rust programming language
Nette security bug CVE-2020-15227
REST API: platform API
Custom web and mail hosting with ISP Config software
Programming in SQL: PostgreSQL, MySQL/MariaDB
HTTPS: secure web
NoSQL database Mongo DB
Connecting to Microsoft SQL Server from Linux
What is the job description of a programmer
Python application localization
Which mail and web hosting to choose
Digispark - Program Atmel ATtiny microcontroller with Arduino IDE
Development for ARM processors with Arduino IDE
How to program the ESP8266 WiFi processor
Open smartphone with Linux - Openmoko Neo FreeRunner

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+