2

How to Install and Create a Node Server on Ubuntu VPS

 2 years ago
source link: https://www.journaldev.com/54410/install-create-node-server-ubuntu-vps
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

NodeJS is a very popular javascript-based backend framework. In this tutorial, we will run how to install NodeJS on an Ubuntu VPS and then create a simple Node HTTP server and access it through the browser.

Installing NodeJS on Ubuntu VPS

I have some apps and websites running on the HostArmada VPS machine. I will use one of those machines for this tutorial. It’s running Ubuntu 20.04 LTS version. We can install NodeJS using the following command.

apt install nodejs

If you are not logged in as root, it might need sudo. In that case, run it as a sudo user.

Install Nodejs UbuntuInstall Nodejs Ubuntu

Checking NodeJS Version

We can check the NodeJS version to verify that the installation is working fine.

Nodejs VersionNodejs Version

Creating a Simple NodeJS HTTP Server

Create a file named “nodeapp.js” and paste the following content.

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Congratulations! node.JS has successfully been installed !\n');
}).listen(3000, "45.79.000.000");
console.log('Server running at http://45.79.000.000:3000/');

Please make sure to change the IP address accordingly. I have changed the last parts to hide my machine IP address.

Run the Node app using the following command:

node nodeapp.js

Your console should print the log message that the server is running.

Accessing NodeJS server in Browser

Go to http://server-ip:3000 and you should see the below output.

Nodejs Http ServerNodejs Http Server

That’s it. We have successfully installed NodeJS application and run a simple HTTP server within 5 minutes.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK