Hello World!

Updated on Oct 6, 2023

The quintessential Node.js application, “Hello World,” is a great place to start. It will teach you what files you need, what code looks like, and how to start the application. Here are the steps to take:

  1. Create a directory for your application. You can name it anything you want, and after that, open it;
  2. Next, you have to create a startup file for the application. It can have any name you like, but it must end in .js. For instance: app.js;
  3. Open the startup file and place the following code in it, then save:

const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end('Hello, World!');
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});

4. Start the Node.js server with this command in your terminal, and you should see it is listening on port 3000: node app.js

5. Finally, using PM2, launch your application with this command: pm2 start app.js

Those are the basic steps you need to take to create the “Hello World” Node.js application. Now, if you want to open the application in a browser, simply go to the address the application is running under to port 3000. For instance, if it is running on a local machine, then go to http://localhost:3000. If you made the file in a directory for a domain name hosted somewhere, simply go to that domain name on port 3000. For instance: http://example.com:3000. You should see the “Hello World” output in your browser.

Let’s break down these steps and discuss them in more detail. Firstly, creating a directory and a file and editing that file should be some things users already know how to do. .js is the extension that all JavaScript files use. Use that extension if you want your application to read a specific file. Now for the meat of the app: the code. We will go line by line.

  • const http = require(‘http’); - This line tells the application to use the http module that comes by default with Node.js. The module is used to create an  HTTP server;
  • const server = http.createServer((req, res) => { … }); - Everything from const server to the semi-colon is the code that creates the HTTP server;
    • To break it down a bit further, what this code does is it takes a callback function which will be executed whenever a request (req) is received and a response (res) is required;
    • req represents the incoming HTTP request, while res represents  the HTTP response that will be sent to the request;
    • The res.write line specifies that the application response header will have code 200 (successful response) and will print out plain text in HTML format;
    • Finally, the res.end line is where the response’s body lies. Whatever is written in it will be the output a user sees. Once the response is sent, the HTTP server severs the connection;
  • const PORT = process.env.PORT || 3000; - As you can probably guess from the line itself, it sets the PORT constant variable. That variable decides which port the HTTP server will listen to, so you can change it with any unoccupied, open port on your system. 3000 is the default one since it is often unoccupied and open. Before it assigns the port, though, the process.env.PORT portion of the line checks if the environment the server and application run on already has a port designated for such tasks. If there isn’t one, it defaults to 3000;
  • server.listen(PORT, () => { … }); - Finally, this is where the application actually starts. The HTTP server is initiated, and it begins listening to the specified port. In the console.log line, you can specify the output you want to appear in your terminal when the server starts successfully. 

And there you have it — a simple application for Node.js, broken down line by line. We hope this clarifies things and will make your programming more easy and satisfying. Read on as we tell you about a method that does not require a terminal to run a NodeJS 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