Introduction

Express is a free and open-source back end web application framework for Node.js, which allows the creation of web applications and APIs in a much easier and cleaner way.

In This tutorial, I will walk you through creating “Hello World!” using Node.js and Express.

Prerequisites

I am assuming you have Node.js and npm installed, If not follow How To Install latest version of Node.js on Ubuntu 20.04.

This tutorial is an extension of the previous tutorial How to Get Started with Express. In the previous tutorial, we have set up an application directory and installed Express. If you don't have set up an application directory and Express installed follow How to Get Started with Express before preceding.

Creating an Express Server

Now Express is installed, create a new file app.js and open it with the code editor of your choice. Then add the following 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 at http://localhost:${port}`)
                        })
                    
                

Let’s break down the different parts of the code.

                    
                        const express = require('express')
                    
                

At the first line, we are importing the main express module from the package we have installed in the node_modules directory.

                    
                        const app = express()
                    
                

The imported module is a function, call this function in the app variable, This is the standard way to configure an Express app.

                    
                        const port = 3000
                    
                

We also need a port number where our application server will listen for incoming requests, this port number we stored in a variable port.

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

By the above line of code, we tell our Express server how to handle GET request, we can also handle other requests in a similar way like POST, PUT, DELETE etc. using app.post(...), app.put(...), app.delete(...) etc.

These functions take two parameters, The first parameter is the URL on which this function will act. In our case, ‘/’ is the root of our website, which is, localhost:3000.

The second parameter is a function with two arguments req and res. req represents the incoming request to the server. res represents the response that will be sent back to the client. In this case, we will send back the response “Hello World!”.

                    
                        app.listen(port, () => {
                        console.log(`App listening at http://localhost:${port}`)
                        })
                    
                

Now we have setup requests and the URL our server will act upon. Now we have to start our server. For this, we pass the port number that we stored in the port variable into the listen function which tells the application which port to listen on.

The second optional parameter is a function that runs when our server starts up. In our case, we will log a string on the console so we can confirm our server is up and running.

Run the application

Now we have the app.js file ready, let’s run our application with the following command:

                    
                        node app.js
                    
                

Now you should see the following string on the terminal which means our application server is up and running.

                    
                        App listening at http://localhost:3000
                    
                

Now visit http://localhost:3000 in your browser, in the browser you will see the response “Hello World!”.

Conclusion

Congratulations!!! you successfully created the “Hello World!” program in Express. In this tutorial, you have seen how to import a module, how to handle incoming requests and how to send a response to the client.