/Express-ES6-Server-Install-Tutorial

In this Tutorial, I show you how to publish your Express Server on your Server.

Express-ES6-Server-Install-Tutorial

In this tutorial, I will show you how to install an ES6 Nodejs Server on a server with Express. On the bottom, there is also a guide on how to run your express app on port 80.

Install

    $ sudo apt update
    $ sudo apt install nodejs npm
    $ curl -sL https://deb.nodesource.com/setup_current.x | sudo bash - #Install the current version of NodeJS
    $ sudo apt install nodejs
    $ nodejs --version

Now we can see the current version (v16) from NodeJS. The right one is for ES6.

Upload

Now upload your project on your server with git or sftp. After that, navigate to the folder

Start

   $ npm i # Install all packages from the package.json
   $ nodemon

Great, it should work now! Look it up in your browser ( http://ip_adress:port or http://domain.tld:port )

Background process

To run your app in the background process we are going to use PM2

PM2 Github Link

Therefore install PM2

   $ npm install pm2 -g

For starting the express server with pm2, run:

   $ pm2 start index.js #Replace the main js file when your name is not index.js

Fine! The Express Server run now in a background process and you can also stop it.

   $ pm2 stop index.js

Set default http port (80)

You probably don't want to run your app on port 3000 or any other, non-default HTTP port. The default HTTP port would be 80 but you can't change the express port to 80. To make it happen you can install a package called libcap2-bin

Website of libcap2-bin

First stop your app and install the package

   $ sudo apt install lib2cap-bin

Now you need to use setcap

   $ sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

After that, you need to edit the file. You can choose your favourite editor like nano or vim

   $ sudo vim index.js

With vim, press i to edit and now change the express port to 80.

Then save the file by pressing ESC, write :X and press enter.

Now start the background process again.

   $  pm2 start index.js

🎉 Nice! Your web page should be available on http://ip_adress or http://domain.tld