{"id":15155549,"url":"https://github.com/akunna1/about-middlewares","last_synced_at":"2026-01-20T05:07:58.311Z","repository":{"id":253160757,"uuid":"842649957","full_name":"akunna1/About-Middlewares","owner":"akunna1","description":"Node-Express Middlewares in depth with explanations and examples","archived":false,"fork":false,"pushed_at":"2024-08-14T19:55:24.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-13T15:18:42.219Z","etag":null,"topics":["backend","express-js","middleware","node-js"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akunna1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-14T19:26:43.000Z","updated_at":"2024-08-17T00:44:06.000Z","dependencies_parsed_at":"2024-08-15T12:34:51.082Z","dependency_job_id":null,"html_url":"https://github.com/akunna1/About-Middlewares","commit_stats":null,"previous_names":["akunna1/about-middlewares"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akunna1%2FAbout-Middlewares","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akunna1%2FAbout-Middlewares/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akunna1%2FAbout-Middlewares/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akunna1%2FAbout-Middlewares/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akunna1","download_url":"https://codeload.github.com/akunna1/About-Middlewares/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247653425,"owners_count":20973827,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend","express-js","middleware","node-js"],"created_at":"2024-09-26T18:23:31.694Z","updated_at":"2026-01-20T05:07:58.306Z","avatar_url":"https://github.com/akunna1.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About Middlewares in Express.js\n\nExpress.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.\n\n---\n\n## 📌 What is Middleware?\n\nMiddleware functions are used to:\n\n* **Modify requests** or responses\n* **Control flow** in your application\n* **Handle errors**\n* **Enhance security**, **log activity**, or **serve static files**\n\nThey are essential to any Express-based Node.js application.\n\n---\n\n## Middleware Function Syntax\n\n```js\nfunction (req, res, next) {\n  // Your logic here\n  next(); // Pass control to the next middleware\n}\n```\n\n* `req`: Request object – holds data about the HTTP request\n* `res`: Response object – used to send data back to the client\n* `next`: Callback function to pass control to the next middleware\n\n\u003e ⚠️ If `next()` is not called, the request will hang.\n\n---\n\n## Common Express Middlewares\n\n| Middleware                               | Purpose                                             |\n| ---------------------------------------- | --------------------------------------------------- |\n| `express.json()`                         | Parses incoming JSON payloads                       |\n| `express.urlencoded({ extended: true })` | Parses URL-encoded payloads (like form submissions) |\n| `express.static('public')`               | Serves static files like HTML, CSS, images          |\n| `cors()`                                 | Enables **CORS** (Cross-Origin Resource Sharing)    |\n| `morgan('tiny')`                         | Logs HTTP request details                           |\n| `helmet()`                               | Sets HTTP headers for security                      |\n| `compression()`                          | Compresses response bodies using Gzip               |\n\n---\n\n## Example: Custom Error Handling Middleware\n\n```js\nfunction errorHandler(err, req, res, next) {\n  console.error(err.stack);\n  res.status(500).send('Something went wrong!');\n}\n\napp.use(errorHandler);\n```\n\nThis function logs the error stack trace and sends a 500 response. Place this **after all other route handlers**.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakunna1%2Fabout-middlewares","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakunna1%2Fabout-middlewares","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakunna1%2Fabout-middlewares/lists"}