https://github.com/akunna1/about-middlewares
Node-Express Middlewares in depth with explanations and examples
https://github.com/akunna1/about-middlewares
backend express-js middleware node-js
Last synced: 5 months ago
JSON representation
Node-Express Middlewares in depth with explanations and examples
- Host: GitHub
- URL: https://github.com/akunna1/about-middlewares
- Owner: akunna1
- Created: 2024-08-14T19:26:43.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-14T19:55:24.000Z (almost 2 years ago)
- Last Synced: 2025-02-13T15:18:42.219Z (over 1 year ago)
- Topics: backend, express-js, middleware, node-js
- Language: JavaScript
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# About Middlewares in Express.js
Express.js is a lightweight web application framework for Node.js. A key feature of Express is the use of **middleware functions**—functions that have access to the `request (req)`, `response (res)`, and `next()` objects in the HTTP lifecycle.
---
## 📌 What is Middleware?
Middleware functions are used to:
* **Modify requests** or responses
* **Control flow** in your application
* **Handle errors**
* **Enhance security**, **log activity**, or **serve static files**
They are essential to any Express-based Node.js application.
---
## Middleware Function Syntax
```js
function (req, res, next) {
// Your logic here
next(); // Pass control to the next middleware
}
```
* `req`: Request object – holds data about the HTTP request
* `res`: Response object – used to send data back to the client
* `next`: Callback function to pass control to the next middleware
> ⚠️ If `next()` is not called, the request will hang.
---
## Common Express Middlewares
| Middleware | Purpose |
| ---------------------------------------- | --------------------------------------------------- |
| `express.json()` | Parses incoming JSON payloads |
| `express.urlencoded({ extended: true })` | Parses URL-encoded payloads (like form submissions) |
| `express.static('public')` | Serves static files like HTML, CSS, images |
| `cors()` | Enables **CORS** (Cross-Origin Resource Sharing) |
| `morgan('tiny')` | Logs HTTP request details |
| `helmet()` | Sets HTTP headers for security |
| `compression()` | Compresses response bodies using Gzip |
---
## Example: Custom Error Handling Middleware
```js
function errorHandler(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something went wrong!');
}
app.use(errorHandler);
```
This function logs the error stack trace and sends a 500 response. Place this **after all other route handlers**.