Setting Up a Node.js Application on Your VPS

Published on
June 1, 2024

Overview

Node.js is a powerful JavaScript runtime built on Chrome'sV8 JavaScript engine. It's used for developing server-side and networkingapplications. This guide will show you how to set up a Node.js application onyour Virtual Private Server (VPS) with HSJ.HOST, enabling you to run JavaScripton the server side efficiently.


Prerequisites

  • A VPS account with HSJ.HOST.
  • SSH access to your server.
  • Basic knowledge of the Linux command line and JavaScript.


Procedure

1. Access Your VPS via SSH

  • Open a terminal or command prompt.
  • Connect to your server using the command ssh user@your_vps_ip, replacing user with your actual username and your_vps_ip with the IP address of your VPS.
       
       

2. Install Node.js

  • Update your package list with sudo apt update.
  • Install Node.js. You can install the version that comes with your distribution's package manager or use a Node version manager (nvm) for more flexibility:
     
  • To install Node.js directly:
         
         

code

sudo apt install nodejs

     
  • To install nvm, follow the instructions on the nvm GitHub page. This allows you to install any version of Node.js.
         
         

bash code

curl -o-https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash nvminstall node # Install the latest version of Node.js

3. Install npm

  • npm (Node Package Manager) is typically installed with Node.js, but if you need to install it separately:
       
       

code

sudo apt install npm

4. Create Your Node.js Application

  • Navigate to the directory where you want to create your app, or create a new directory:
       
       

Copy code

mkdir my_node_app && cd my_node_app

  • Initialise a new Node.js application:

csharpCopy code

npm init -y

  • This command creates a package.json file with default values. Edit this file to suit your application's requirements.
       
       

5. Install Express Framework (Optional)

  • Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. To install Express:
       
       

css code

npm install express --save

  • Create an index.js file in your application directory:
       
       

bashcode

touch index.js

  • Open index.js and write a simple Express server:
       
       

Javascript code

const express = require('express'); const app = express();const port = 3000; app.get('/', (req, res) => res.send('Hello World!'));app.listen(port, () => console.log(`Example app listening athttp://localhost:${port}`));

6. Run Your Node.js Application

  • Start your application with Node.js:


Code

node index.js

  • Your app will now be running on the specified port, and you can access it via your web browser by navigating to http://your_vps_ip:3000.

 

Conclusion

You have successfully set up a Node.js application on yourVPS with HSJ.HOST. This setup forms the basis for developing powerfulserver-side applications in JavaScript. As you become more familiar withNode.js and its ecosystem, you can explore more complex setups, including usingdatabases, managing environment variables, and deploying production-readyapplications.

Join our newsletter

Unlock Exclusive Offers Subscribe to Our Newsletter!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.