Hello World Application with Node.js and Express

Hello World Application with Node.js and Express

20/09/2026

In  Setting Up the Environment | Node & Express, I created the "app.js" file — the entry point of the project. I can run it right now to see what it looks like, but it will only be available locally. At the end of this project, I will explain how to run it on the Internet so that anyone will be able to visit it.

In the article mentioned above, we installed Express. Express allows us to create a local web server:

// app.js

const express = require('express') // Import Express
const app = express() // Web application

const port = 3000 // Port number

// Start the web server on the port initialized above
app.listen(port, () => {
// Display the server's address in the console
console.log(`The server is running on http://localhost:${port}`)
})


I chose port 3000 because it's usually not used by other services and is very common in development. Other common ports are 8080, 3001, and 9999. You can choose any port from 1024 to 49151, but first, you must make sure that it's available and isn't used by another service on your device.

The "listen" function here takes two arguments: the port and a function that performs an action when the server starts.

Run it with the following command in the VS Code terminal:

node app.js


Hold the "Ctrl" key and click on the address displayed in the terminal (http://localhost:3000). It will open a browser page. It may ask you to choose a browser, or open it using a built-in feature called the "Integrated Browser". I use Firefox for this.

You can see the "Cannot GET /" message on the page, which means that we haven't added any handler for this path/route yet. Let's display "Hello, World!" on this page. Add the following code to the "app.js" file:

app.get('/', (req, res) => {
res.send('Hello, World!')
})


Here we tell the program to send the "Hello, World!" message to the user when they visit this page. Here we use the "GET" HTTP method, specifying that this request is about retrieving information. It can also be "POST", "PUT", "DELETE" and so on, depending on the purpose of the request.

The "req" and "res" parameters specify the input and output, respectively. We are using the "res" parameter to send the output message to the user.

It's possible that you will not see any changes, and you will need to stop the server and run "node app.js" again. To stop it, press the "Ctrl + C" combination in the terminal. To fix this, when I start the server, I use the "--watch" option to the command:

node --watch app.js

So from now on, to see any changes, you can just reload the page.

Now everything is ready, and the simple "Hello World" application is working. One final thing to make it slightly simpler — I don't like writing "node --watch app.js" every time, so I use a little bit shorter "npm run dev" command. To do the same, go to "package.json", find the "scripts" section, and add the "dev": "node --watch app.js" line here:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node --watch app.js" <-----  HERE
  },
...


I already had the default "test" line, but I never use it. You can write here whatever script you want. Now you can write "npm run dev" in the terminal, and it will run the server: