{"id":15496298,"url":"https://github.com/imranhsayed/express-app","last_synced_at":"2026-02-13T20:59:47.348Z","repository":{"id":97032557,"uuid":"187775771","full_name":"imranhsayed/express-app","owner":"imranhsayed","description":":zap: A demo app for Express","archived":false,"fork":false,"pushed_at":"2022-12-10T15:06:09.000Z","size":551,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-03T12:06:26.009Z","etag":null,"topics":["custom-middleware","express","express-js","javascript","logger-middleware","middleware","nodejs","stream"],"latest_commit_sha":null,"homepage":null,"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/imranhsayed.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":"2019-05-21T06:32:57.000Z","updated_at":"2021-11-10T13:37:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"85e64e7d-902d-4d12-8b23-51f8da45567f","html_url":"https://github.com/imranhsayed/express-app","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/imranhsayed/express-app","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fexpress-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fexpress-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fexpress-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fexpress-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imranhsayed","download_url":"https://codeload.github.com/imranhsayed/express-app/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imranhsayed%2Fexpress-app/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29417705,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T06:24:03.484Z","status":"ssl_error","status_checked_at":"2026-02-13T06:23:12.830Z","response_time":78,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["custom-middleware","express","express-js","javascript","logger-middleware","middleware","nodejs","stream"],"created_at":"2024-10-02T08:24:26.564Z","updated_at":"2026-02-13T20:59:47.334Z","avatar_url":"https://github.com/imranhsayed.png","language":"JavaScript","readme":"# Express App\n\n## Description :clipboard:\n\u003e A demo app for express\n\n## Installation :wrench:\n\n1. Clone this repo in `git clone https://github.com/imranhsayed/express-app`\n2. `git checkout branch-name`\n\n# FAQs\n\n## 1. What are Middlewares ? :vertical_traffic_light:\n\n\u003e * Middlewares are functions added to the stack, that have access to the request and response object.\n\u003e * They are executed sequentially. We can do things like validation, authentication, data-parsing etc inside of each middleware.\n\u003e * When the request comes in it passes through each of the middleware, before reaching the actual route. \n\u003e * An express application is a stack of middleware running one after the other.\n\u003e * The `next()` must be called at the end of every middleware ( function ), to move the processing to the next middleware in the stack.\n\n## 2. Why do we use body-parser middleware?\n\u003e * The `body-parser` middleware parses the form data, so that we can read it.\n\u003e * Calling this function and setting extended to false, `bodyParser.urlencoded( {extended: false} )`\n forces the use of node's native queryString module, which parses the data . It returns a middleware function that parses data.\n \n ```ruby\n const bodyParser = require( 'body-parser');\n app.use( bodyParser.urlencoded( {extended: false} ) );\n ```\n \n## 3. When is response.sendStatus() used?\n \n * `response.sendStatus( 200 )` is used in our express routes, when we don't want to send a body along with the response.\n  It sets the response body to 'OK' by default. \n  \n## 4. How do we handle Multiple Route Instances?\n\n * When we have multiple routes ( like get, post, delete ) for the same route url `/posts` , we can chain them together using `app.route()` like so:\n \n```ruby\n app.route( '/posts' )\n    .get( ( req, res ) =\u003e ... )\n    .post( ( req, res ) =\u003e ... )\n ```\n \n * The `app.route( $path )` takes the route path and returns a route object that handles all request to the given `$path`\n * Chaining means calling the next function on the return value of the previous function.\n * The lines starting with dot indicate the function calls on the object returned from the previous call.\n \n ## 6. How will you extract routes to modules?\n \n * So that our main file does not become too long when we have many routes, we can extract routes by breaking them into modules.\n * Create a dedicated folder for all your routes and create a separate file for each time of routes. E.g. dir name routes and filename\n will be posts.js inside of it, where we define all routes related to post\n * Now in your routes/posts.js file, create an instance of a router using `const router = express.Router();` and export router at the bottom\n * Then we can define all our routes using the router instance like `router.get()`, or chain them using `router.route()`\n * The path `/` is relative to where this router will be mounted in your main entry point file `server.js`\n \n ```ruby\n const express = require( 'express' );\n const router = express.Router();\n \n router.route( '/' )\n    .all( (req, res) =\u003e ... )\n    .get( (req, res) =\u003e ... )\n    .post( (req, res) =\u003e ... );\n    \n router.get( '/:id', ( req, res ) =\u003e ... );    \n \n module.exports = router;\n ```\n \n * Notice that `router.all()` is used for all types of routes ( get, post, delete etc ) to that url.\n * Finally we require this file `routes/post.js` into our main entry point file `server.js`\n \n```ruby\nconst posts = require( '/routes/posts' );\napp.use( '/posts', posts );\n```\n\n* Another Example:\n\n```ruby\n\t// server.js\n\tconst express = require( 'express' );\n\tconst app = express();\n\tconst port = 3000;\n\t\n\tconst products = require( './routes/api/products' );\n\t\n\tapp.use( '/api/products', products );\n\t\n\tapp.listen( port =\u003e console.warn( `Ready on http://localhost:${port}` );\n\t\n\t\n\t\n\t// routes/api/products.js\n\tconst express = require( 'express' );\n\tconst router = express.Router();\n\t\n\trouter.get( '/getProducts', ( req, res ) =\u003e ... );\n\t\n\tmodule.exports = router;\n``` \n \n\n## Branch Information :computer:\n\n### 1. [static-middleware](https://github.com/imranhsayed/express-app/tree/static-middleware)\n\n\u003e The `express.static()` middleware that comes shipped with express, automatically servers the `index.html` on root url `'/'` without having to \ncreate a route using `app.get( '/' )` \n\n```ruby\napp.use( express.static( 'public' ) )\n```\n\n### 2. [custom-middleware](https://github.com/imranhsayed/express-app/tree/custom-middleware) \n\u003e * Creates a custom middleware called loggerMiddleWare.\n\u003e *\tRun `npm run dev`\n\u003e * Open browser at `http://localhost:5000`\n\u003e * Every time you refresh the page, a GET request to `http://localhost:5000/` is made \nand the middleware intercepts that request and when the response for that request is finished\nit calculates the time it took for that request to complete and prints that.\n\n\u003e * Also adds the message to a `log.txt` file  \n  \n### LoggerMiddleWare Demo :video_camera:\n\n![](loggerMiddleWare.gif)\n\n### 3. [query-params](https://github.com/imranhsayed/express-app/tree/query-params)\n\n\u003e * An example to create a route and get the post data at `/posts`\n\u003e * If the user adds a query string `?limit=2` in the route, then it should return those many post items\n\n### Request with Query String Demo :video_camera:\n\n![](query-string.gif)\n\n### 4. [dynamic-routes](https://github.com/imranhsayed/express-app/tree/dynamic-routes)\n\n\u003e * An example to create a dynamic route at `/posts/:name`\n\u003e * Request to `/post/movies` will return 'I love movie blogs'\n\u003e * Request to `/post/software` will return 'I like Express'\n\u003e * Request to name that's not available in post object, like `/posts/xyz` will return 'No description found for xyz'\n\n```ruby\nconst posts = {\n\t'movies': 'I love movie blogs',\n\t'games': 'The best game of the year is God of War 4',\n\t'software': 'I like Express'\n};\n\n// Dynamic Route\napp.get( '/posts/:name', ( req, res ) =\u003e {\n\n\tconst description = posts[ req.params.name ];\n\n\t// If the value of 'name' passed in the route does not exist as posts property\n\tif ( ! description ) {\n\t\tres.status( 404 ).json( `No description found for ${req.params.name}` );\n\t} else {\n\t\tres.json( description );\n\t}\n} );\n```\n\n### Dynamic Routes Demo :video_camera:\n\n![](dynamic-routes.gif)\n\n\n## Common Commands :computer:\n\n* `npm run dev` Starts Node server at `http://localhost:5000`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimranhsayed%2Fexpress-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimranhsayed%2Fexpress-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimranhsayed%2Fexpress-app/lists"}