Express.js Basics (Middleware, Routing, CORS)
Express.js is the most popular web framework used with Node. It helps you build APIs, handle routes, manage middleware, and create backend applications faster and cleaner. From simple portfolio APIs to production-level SaaS apps, Express powers everything. But beginners often get confused with technical jargon like middleware, routing, and CORS.
In this blog, we will learn about the basics of Express.js, including middleware, routing, and CORS.
What is Express.js?
Express.js is a minimal and flexible Node.js web framework used to build web applications and APIs. Node gives you the power, while Express gives you structure. Without Express, building a server in Node feels complicated and repetitive. With Express Node Framework, you can:
Create APIs easily
Handle routes cleanly
Manage requests and responses
Add middleware in a structured way
Install Express:
Create <server.js>:
Run:
Now open <http://localhost:3000>
What is Express Middleware in Node?
Middleware in Express.js is just a function that runs before your final route sends a response. That’s it. When a user makes a request, Express does not directly jump to your route. It passes that request through one or more express middleware functions first.
In Express, a middleware function gets three things (req, res, and next). The important one is <next()>. If you call <next()>, the request moves forward. If you don’t, the request stops there.
Whenever someone visits the homepage, the middleware runs first and logs the time. Then next() allows the request to reach the route handler.
That’s how express js middleware works in real applications. Everything, including authentication, logging, validation, and error handling, is built using middleware.
Think of it like a checkpoint system. Every request must pass through certain checks before reaching its final destination.
How Does Middleware Work?
To really understand Express middleware, you need to understand the request flow inside the Express Node framework.
When a client sends a request:
The request enters your Express app
It moves through the middleware functions one by one
If all middleware calls <next()>, it finally reaches the route handler
A response is sent back
If any middleware does not call <next()> and instead sends a response, the flow stops there. That’s the core logic behind express middleware.
Flow Example:
Console output when you hit the </>:
First middleware. Second middleware. Then the route sends the response. This is called a middleware stack.
Besides this, there are different types of middleware in ExpressJS:
Application-level middleware (<app.use()>)
Router-level middleware
Built-in middleware (<express.json()>)
Error-handling middleware
How to Use Express Middleware?
In real projects, you don’t just write random <express middleware>. You use it to solve the actual backend problems. Here’s how you actually use middleware in everyday development.
1. Using built-in Middleware
Express comes with built-in middleware functions. The most common one is:
This allows your app to read JSON data from the request body. Without this, <req.body> will be <undefined>.
Example:
If you send JSON from Postman, it will not work properly. This is one of the most important express functions beginners forget.
2. Creating Custom Middleware
You can create your own middleware for logging or authentication.
Now every request will pass through this logger.
3. Using Middleware for Specific Routes
You don’t always want middleware for the entire app.
Here, the logger runs only for </admin>.
Routing in Express
Routing is how your backend decides what to do when a specific URL is requested. In Express.js, routing is clean and beginner-friendly. That’s why the express node framework is so popular.
Basic Route Example
<GET/> handles homepage results
<POST/login> handles login submissions
Route Parameters
You can capture dynamic values in URLs.
If someone visits </users/101>, Express captures <101>.
This is heavily used in APIs.
Express Router
In real projects, you don’t keep everything in one file.
Then in the main file:
Now </user/profile> works.
CORS in Express
If you have ever seen this error - “Access to fetch at ‘http://localhost:3000’ from origin ‘http://localhost:5173’ has been blocked by CORS policy…,” you are not alone.
CORS stands for Cross-Origin Resource Sharing. It’s not an Express problem; it is a browser security rule. When your frontend (say React on port 5173) tries to call your backend (Express on port 3000), the browser blocks it unless your backend allows it.
Installing CORS
Then use it:
Now your frontend can call your backend without errors.
Allow Specific Origin
Instead of allowing everyone:
In production, you should always restrict origins.
Final Words
If you understand middleware, routing, and CORS, you understand the foundation of the entire Express Node framework. Most beginners try to jump directly into building full-stack projects. But backend confidence comes from mastering the basics first:
Middleware controls the request flow
Routing decides where the request goes
CORS allows the frontend and backend to communicate safely
Open VS Code, create a small API, break it, fix it, add middleware, restrict CORS, create routes, and test using Postman. Backend development becomes easy once the flow clicks in your mind.
Frequently Asked Questions (FAQs)
Q1. What is middleware in Node.js?
Ans. Middleware in Node.js is a function that runs between the incoming requests and the final response. It can modify the request, validate data, authenticate users, or pass control to the next function using <next()>.
Q2. What are express functions?
Ans. Express functions refer to methods like <app.get()>, <app.post()>, <app.use()>, <app.listen()>. These functions help define routes, apply middleware, and start the server in an Express Node app.
Q3. Is Express.js relevant in 2026?
Ans. Yes, Express.js remains one of the most widely used backend frameworks in the Node ecosystem. It is lightweight, flexible, and heavily used in startups and production APIs.