What is express.js?
Express.js is the part that sits between your frontend and your database.
It receives requests from the frontend, processes them, talks to the database if needed, and sends back a response.
It works a lot like PHP or C#, if you've used those before.
To start using Express, you need Node.js and npm (Node’s package manager) installed. You can download them here: getNode()
Quick note: Node.js is a software that lets JavaScript run outside of the browser.
JavaScript was originally only allowed inside browsers.
Then, open your terminal or command prompt and follow these steps:
# Create a new folder for your project
mkdir my-new-express
cd my-new-express
# Initialize a new Node.js project (creates package.json)
npm init -y
# Install Express
npm install express
Now, lets get to the coding part.
The first line you are going to write is:
let express = require('express')
This line tells Node.js to look inside your project’s node_modules folder for the Express package.
It then loads Express and gives you back an function you can use to create your web server and handle requests.
If you're familiar with object-oriented programming (OOP), you can think of the express variable as a class - like a blueprint for creating web servers.
When you call it, it creates an instance, and that instance is your actual web server.
We call it like this:
let app = express()
Now, the variable app is your server, built based on the blueprint that is the express function.
Note: Calling the express variable a class is just an analogy to make it easier for beginners to understand. It is infact a factory function.
Let’s Understand What a Web Server is. Imagine you click the login button on Facebook. A lot happens behind the scenes, but eventually, your login request reaches the server.
In this case, the server is where your Express app lives.
The server’s job is to listen for different types of requests (like login, signup, fetch data) and handle each one with specific rules.
For example, the login request will be handled by a special part of your server that checks the username and password.
So basically, The server is a set of rules that decides what to do with each request it receives.
If this feels confusing, don’t worry — let’s learn with an example.
Imagine you’re using a food delivery app like Swiggy or Uber Eats. You click on “See Menu” for a restaurant.
Your phone sends a request to the server asking, “Hey, what food items do you have?”
The server has a rule to handle this specific request. For example:
If the request asks for the menu, the server sends back the list of food items.
Similarly, there are rules for other actions. Like when you click "Place Order" — there's a specific rule in the server that handles ordering.
It might check if the restaurant is open, calculate the total price, and confirm your order.
Same thing happens when you click "Login" — there's another rule that checks your username and password.
Each button or action in your app has its own rule on the server that decides what to do when that specific request comes in.
Here is the example of the rule we talked about:
app.post("/api/login", function(request, response){
const {username, password} = request.body;
if (username === "john" && password === "123456") {
response.send("Login successful!");
} else {
response.send("Invalid username or password");
}
});
This entire block of code is one rule. It tells the server: "When someone sends a request to /api/login, run this function." (basically when someone clicks the login button)
The function grabs the username and password from the request, checks if they're correct, and sends back a response using response.send().
Don't worry about understanding every detail right now — like what request.body means or how the if statement works. We'll cover those later.
The main point is: this whole thing is a rule that handles login requests. When someone clicks the login button in your app, this rule runs and decides what to send back.
There will be another endpoint to handle signup, and it might look like:
app.post("/api/signup", function(request, response){
// signup logic here
});
And so on for different actions in your app.
More on this in the next post, but we'll get to the more practical side of things. There are some unexplained parts here which I'll cover in the next article.
Oh, and one more thing - your server needs to actually 'listen' on a port to work. But more on that later as well.
Top comments (0)