How to Build a simple Web Server using Node.js and Express
By: Gianmarco Ebeling
Tags: Nodejs, Backend Development, Web Server, Expressjs, JavaScript

Let’s set up this server!

Once we installed Node.js on our machine, we are ready to build our simple server.

First, we will create a new folder on the desktop and a new JavaScript file using the command line:

First, open the terminal. To find out in which directory we are we can type pwd (print working directory). If we are not already on the desktop, we can change the directory with cd Desktop/ .

(print working directory). If we are not already on the desktop, we can change the directory with . To create the new folder type mkdir and the name of the folder. I will call mine “server”.

and the name of the folder. I will call mine Let’s now change the directory again and inside of our new folder we can create a new JavaScript file named index.js . In this case, we will use the command touch .

Now, we will have to set up NPM, which is a package manager for external modules. NPM stands for Node Package Manager, and it’s currently the largest software registry in the world.

We initialize NPM typing npm init .

Now we set up our NPM. We can decide a name for the package, the version of the project, the description, and the entry point (that refers to our main Javascript file, in our case index.js ). We can ignore the other settings and hit enter until we have created our package.json.

Let’s open our project in the text editor. The shortcut to do so in the command line is simply typing the name of your text editor (for example Atom) followed by a dot.

I will open the project in Visual Studio Code typing code .

We are now ready to install express.

Type npm install express .

The next step is to go over to our JavaScript file and require the package “express”. We can then create a new const called app , which is set equal to express . Now we can use one of its methods, called listen . This tells app to listen to a specific port for any HTTP requests that get sent to our server. A port is like a channel that we’ve tuned our server to. For this example, I chose the port 3000. Let’s then add a callback function to this method that consoles a message: “Example of a server running at http://localhost:3000".

Now we can save the file and type node index.js in the command line. This will print out our message and start the server.

We have just created our Web Server!

Now the problem is that if we go to http://localhost:3000, it will show an error: cannot GET / .

We have to write some extra code so that our server responds when a browser is making a request to it. First, let’s stop the server with ctrl + C and open again our index.js .

We now have to add some lines of code to this JS file. Just above app.listen , we have to add app.get . This method allows us to specify what should happen when a browser makes a get request and gets in touch with our server.

app.get ('/', (req, res) => {

res.send('Hello! Everything is working fine!')

})

The first parameter is the location of the get request. Going to http://localhost:3000, the get request is being sent to the route of our web site, which is represented by “/”.

Now when that get request happens, we can trigger a callback function that will have two parameters: request and response. So currently our server isn’t responding with anything. We can use the send method to send a response, which will be the text: “Hello! Everything is working fine!”.

Let’s save the file and run the server again with node index.js in the command line.

Now, if we go to http://localhost:3000, we finally got back our message!