Node.js - Building a Proper Application - Step 1 - Installing Prerequisites

Updated on Oct 6, 2023

Firstly, create a directory for your application, or use the directory for the domain you want the application to appear on, but remember it has to be empty. In it, create an index.js file. The file is where most of the code for the application will be contained. Afterward, open your terminal of choice (we used the one that comes with cPanel) and navigate to the directory. When in it, install Express, as it is necessary for the Node server.

  • npm install express --save

When Express is installed, open the index.js file either in your terminal or via your file manager or browser. Place this code in it.


//require the just installed express app
var express = require('express');

//then we call express
var app = express();

//takes us to the root(/) URL
app.get('/', function (req, res) {

//when we visit the root URL express will respond with 'Hello World'
  res.send('Hello World!');
});

//the server is listening on port 3000 for connections
app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
});

This code may seem familiar because it is a variation on the “Hello World” code from earlier. The reason we are doing this is to test the server and ensure that it is running correctly. Start the server with this command. 

  • node index.js

The server has successfully started if you see an output that tells you the app is listening on port 3000. To ensure it works, open an internet and go to one of these addresses.

  • http://localhost:3000 - Use this if you are hosting your application on a local device and not online;
  • http://domain.com:3000 - Replace domain.com with the domain name you have chosen to host your app. Use this if you are hosting it on a domain that is online.

If you see “Hello World” in your browser, everything is going great. There isn’t really anything that can go wrong in this part, so let’s move to the actual application.

On this page...

    Node.js Hosting

    • Multiple Node Versions
    • 24/7 Node.js Support
    • Free Domain Transfer
    • Hack-free Protection
    • Fast SSD Storage
    • Free Node.js Transfer
    • Free Cloudflare CDN
    • Immediate Activation
    View More