In the last lecture, you saw the server-side analogy of the Hello, world that you wrote as your first task in VSCode. Our program was able to process the request and return a response, which was displayed by the browser.

Today, you are implementing your first Node.js backend!

Express.js and npm modules

When talking about backend development in Node.js, in most cases we mean working with Express.js. It is an open-source framework that is distributed using npm.

If you don’t know how to work with npm packages yet, I recommend reading these two articles:

To add Express.js to your project, you must first install it:

npm install --save express

The --save flag means that express will be added to the dependencies section in the package.json file, where the dependencies of your Node.js project are stored.

After installation, add import:

import express from 'express';
const server = express();

Express.js and Node.js hide from us a lot of complexities associated with handling HTTP requests and all the low-level networking.

To add a handler for GET requests, we need to call the function server.get(route, handler), where route is a string describing the path to the resource, and handler is a function that will be executed when the GET request hits the route.

server.get('/', (req, res) => {
  return res.send('Hello, Express.js!');
})

We use / as the path, but it could be something else, like /about, /blog, or /profile.

The handler function takes two parameters req and res. They are very important, but for now, remember that you can send the response using the res.send() function. If you don’t call res.send() but just write return 'hello world'; then the server will not be able to respond.

Next, we need to start the server.

Let’s use the listen(port, callback) function. It expects 2 parameters. You can choose any free port in your system as a port param, and callback is a function that will be executed after the server is started. Usually, it displays information that the server is running on the specific port and everything is fine.

const port = 8080;

server.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

If you follow the instructions, save the javascript code to the file index.js and write node index.js, then you will start your first server written using the framework Express.js.

While it will not yet be available to the outside world, if you try to navigate to localhost:8080 in your browser, you will see that the server responds with Hello, Express.js!.

Questions?

— How to understand which port in the system is free?
— To keep things simple, just choose ports between 3000 and 9000.

— What is localhost?
— This is your computer’s abbreviated address. Also, you can use the full IP address 127.0.0.1. It is reserved and always points to the local computer. A “self-reference” of a sort.

— What happens if I try to navigate to localhost:8080/about but don’t implement that route in server.get?
— The server will return an error 404, which means that the requested resource was not found.