Skip to content

Node.js

Node.js is an open, cross-platform development environment.

Initial configuration

To run an application based on Node.js first make sure you have a proper preconfiguration. Before proceeding to the next steps, make sure that:

  • The domain is properly added and configured in our DNS system.
  • WWW site has been added with the indication of the nodejs type in the chosen type.
  • The following commands have been carried out:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global' 
echo 'export PATH=~/.npm-global/bin:~/bin:$PATH ' >> $HOME/.bash_profile && source $HOME/.bash_profile

Versions of Node.js

The following Node.js versions are available on Serv00.com servers:

  • v12.22.1 - node12 and yarn12 binaries
  • v14.21.1 - node14 and npm14 binaries
  • v16.20.0 (default) - node and npm16 binaries
  • v18.16.0 - node18 and npm18 binaries
  • v20.4.0 - node20 and npm20 binaries

Change of Node.js version under the commands node andnpm - in the example the version v18 was set, to indicate a different one, the digit next to node and npm should be changed accordingly:

mkdir -p ~/bin && ln -fs /usr/local/bin/node18 ~/bin/node && ln -fs /usr/local/bin/npm18 ~/bin/npm && source $HOME/.bash_profile

To change the version of Node.js again this time to v16, just run the command with the digit change:

mkdir -p ~/bin && ln -fs /usr/local/bin/node16 ~/bin/node && ln -fs /usr/local/bin/npm16 ~/bin/npm && source $HOME/.bash_profile

Node.js project

The main directory of the Node.js project (i.e. the directory where the app.js file is located) must be /usr/home/LOGIN/domains/DOMAIN/public_nodejs. You should also delete the index.html file with the command:
rm /usr/home/LOGIN/domains/DOMAIN/public_nodejs/public/index.html

Node.js modules

To install the selected Node.js module, use the command npm install -g MODULE_NAME. It is recommended to install binary modules in the home directory. To be able to use them, you must turn on Binexec and login again.

Warning

In order for some npm modules to compile correctly, the following variables must be set:

export C=clang
export CXX=clang++

and in the event of an error, check:

export CC=gcc
export CXX=g++

Preparing the Node.js application

A new Node.js project should be created in the directory /usr/home/LOGIN/domains/DOMAIN/public_nodejs - the easiest way to create an appropriate project is to create an app.js file:

var http = require('http');
var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end("hello world!\n");
});
server.listen(3000);

The port 3000 visible in the last line will not open the socket on port 3000. ThePhusion Passenger automatically creates a socket through which it communicates with the application. Port reservation is not needed when running the application with nodejs type.

Existing Node.js project

The existing Node.js project should be placed as the directory /usr/home/LOGIN/domains/DOMAIN/public_nodejs. If the project is not named public_nodejs, its name should be changed. You also need to modify the app.js file to execute the application. Usually, apart from the corresponding require and the listen() setting, the app.js file does not need any more items. You also need to install the missing modules with the command: npm install

Restart the application

Node.js application can be restarted using our service management panel in the WWW tab, or using the Devil account management system command: devil www restart DOMAIN.

Process limit for Phusion Passenger

Possibility to set the number of processes for nodejs pages. Command syntax: devil www options DOMAIN processes <Quantity>

Where:

  • DOMAIN - stands for the nodejs type WWW domain
  • QUANTITY - number of processes in the range from 1 to 80% of system processes of the hosting account

The option is also available in the DevilWEB panel:
WWW websitesDetailsNumber of processes ➡ enter a new value and save the changes with the 💾 Save changes button.

Static files

All files under /usr/home/LOGIN/domains/DOMAIN/public_nodejs/public are handled as static files. It is best to put all pictures, scripts, styles, etc. in this directory. Requests to files in this directory will not be processed by Node.js processes and will not burden the interpreter. For example, the file /usr/home/LOGIN/domains/DOMAIN/public_nodejs/public/robots.txt will be available at http(s)://DOMAIN/robots.txt.

Variables

Setting a variable from the SSH with visibility for the application launched by Passenger server is possible by adding variables to the ~/.bash_profile file (e.g. export TESTENV = 1).

Warning

Variables will not be used from ~/.bashrc and ~/.shrc files

Logs

Warning

Do not delete the logs directory, it may cause problems withNode.js, Ruby and Python pages.

The error logs are saved to the error.log file in the /usr/home/LOGIN/domains/DOMAIN/logs/ directory.

App lifetime

If there are no requests to the site for 24 hours, the application is automatically shut down and started again automatically on the first next visit. In the event of a change in the website configuration, the application is also automatically shut down and launched the next time the website is accessed for the first time.

External references