NodeJS is an asynchronous, event-driven JavaScript runtime environment.

 

The primary benefit of using NodeJS as your back-end platform is its ability to incorporate concurrent connections without over-utilizing I/O operations, thus saving a lot of overhead in CPU power and allowing much greater scalability. Additionally, it’s fully compatible with all your favourite JavaScript frameworks, libraries, and extensions, such as React, JQuery, Angular, and more.

 

Prerequisites:

  1. Ubuntu 18.04 server (although desktop should still work)
  2. Non-root user
  3. General understanding of Command-Line tools

 

To begin, let’s make sure the operating system is up-to-date.

sudo apt update && sudo apt upgrade -y

 

Install Using Apt

 

Ubuntu 18.04 apt repositories include a relatively recent version of NodeJS that can be installed using apt directly.

sudo apt install -y nodejs

 

This will also install npm, the Node Package Manager. npm is used to install node modules, frameworks, apps, etc. You can now test the install by passing the version flag.

node -v
v8.10.0

 

Testing npm‘s version as well:

npm -v
3.5.2

 

This is enough to begin working with NodeJS. But you may want to have a newer version available or have more control over the version in general– and that’s where the node version manager comes into play.

 

Installing using Node Version Manager

 

If you prefer to have more control over the version installed, and even in use at any given time Node Version Manager (nvm as referenced in commands) provides that control.

 

Let’s check out that method.

 

First, we have to actually install nvm. The best way to install nvm is from the source. You can find the latest install commands for nvm here, which can be used to both install and update your nvm instance.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

 

The install script will run, asking you to close and reopen your terminal to enable nvm. Do so, after which you can check to make sure the command works, and see what nvm version is currently installed.

nvm --version
0.34.0

 

I highly encourage you to read through the help text of nvm (run nvm --help), as it provides functionality that many will find extraordinarily useful. However, for now, let’s set up the latest version of NodeJS.

nvm install --lts

 

This is a simple command to tell nvm to install the latest version of the current LTS (long-term support) version of NodeJS. If you have not previously used nvm to install a NodeJS version, this command will furthermore set up the “default” alias to this version. If you wish to install a specific version, you can specify the version you desire, as well.

nvm install 10.10.0

 

Once you have the version you want to be installed, use nvm to switch to that version as the actively used version.

nvm use 10.10.0
Now using node v10.10.0 (npm v6.4.1)

 

OR

 

nvm use default
Now using node v10.16.1 (npm v6.9.0)

 

Getting Started using NodeJS

 

Your NodeJS framework is ready to run your app.

 

Here’s a demo “hello world” app, as displayed on the NodeJS getting started page. Replace SERVERIPADDRESS with your server’s public-facing IP Address, or if you’re running this on a local desktop version of Ubuntu 18.04, you can use “127.0.0.1” as depicted on the getting started guide.

const http = require('http');

const hostname = 'SERVER_IP_ADDRESS';
const port = 3000;

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}/`);
})

 

Save this as app.js in a folder on your NodeJS host, then run:

node app.js
Server running at http://SERVER_IP_ADDRESS:3000/

 

You should now be able to navigate to your VPS IP Address on port 3000 and view the text displayed.

Was this answer helpful? 0 Users Found This Useful (0 Votes)