A Beginner's Guide to Express.js

Take a look at what we created in the previous lesson:

javascript
1import { createServer } from "http";
2import { writeFile, readFile } from "fs";
3
4let server = createServer((request, response) => {
5  if (request.method === "GET") {
6    . . .
7  } else if (request.method === "POST") {
8    . . .
9  }
10});
11
12server.listen(8000);
13
14console.log("Listening! (port 8000)");

For a single route application (/), we had to consider two different request methods, GET and POST.

Imagine the application has several other routes, and for each route, we have to deal with at least two request methods. This app will become very difficult to maintain. Not to mention we haven't discussed the security measures, testing and logging practices, how to split the frontend and backend code, and so on.

This is why frameworks such as Express.js are created to speed up our development cycle.

It doesn't matter what kind of application you are building, there are always some tools and components that you are going to need, such as routing, database integration, template system, and so on. This is what Express is for, it is a set of tools and components that you are going to need when building web applications.

Installing Express.js

To initialize a new Express application, create a new work directory. Of course, you could do things through the graphical interface, but after investing so much time and resources trying to become a programmer, let's do things the programmer way.

Open the terminal and run the following command:

bash
1mkdir <work_dir>

Wait, there is more!

You need an account to access the rest of this lesson. Our course is 50% off for limited time only! Don't miss the opportunity!

🎉 Create an Account 🎉