├── .gitignore ├── index.js ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | 4 | app.listen(3000, () => { 5 | console.log('Server is running on port 3000'); 6 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express.js-middleware-and-queryparameter-handling-assignment", 3 | "version": "1.0.0", 4 | "description": "This assignment focuses on creating and using middleware in an Express.js application. You will build an application that handles query strings and route parameters using middleware functions for validation and response control.", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "nodemon index.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/asif-daffodil/Express.js-Middleware-and-QueryParameter-Handling-Assignment.git" 13 | }, 14 | "keywords": [], 15 | "author": "", 16 | "license": "ISC", 17 | "bugs": { 18 | "url": "https://github.com/asif-daffodil/Express.js-Middleware-and-QueryParameter-Handling-Assignment/issues" 19 | }, 20 | "homepage": "https://github.com/asif-daffodil/Express.js-Middleware-and-QueryParameter-Handling-Assignment#readme", 21 | "dependencies": { 22 | "express": "^4.21.2", 23 | "nodemon": "^3.1.9" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Express.js Middleware and Query/Parameter Handling Assignment 2 | 3 | ## Overview 4 | This assignment focuses on creating and using middleware in an Express.js application. You will build an application that handles query strings and route parameters using middleware functions for validation and response control. 5 | 6 | ## Project Structure 7 | ``` 8 | - /middlewares 9 | - checkAge.js 10 | - validateName.js 11 | - /public 12 | - (static files like index.html, style.css, etc.) 13 | - index.js 14 | - README.md 15 | ``` 16 | 17 | ## Instructions 18 | 19 | ### 1. Set Up Express.js Server 20 | - Create an `index.js` file to set up a basic Express.js server. 21 | - Serve static files from the `public` directory using `express.static()`. 22 | 23 | ### 2. Middleware for Name Validation 24 | - Inside the `middlewares` directory, create a file named `validateName.js`. 25 | - In this middleware, check if the `name` query string is present when accessing the `/contact` route. 26 | - If `name` is missing, respond with: `"Please provide your name."` 27 | - If `name` is present, pass control to the next middleware or route handler. 28 | 29 | ```javascript 30 | // validateName.js 31 | const validateName = (req, res, next) => { 32 | if (!req.query.name) { 33 | return res.send("Please provide your name."); 34 | } 35 | next(); 36 | }; 37 | 38 | module.exports = validateName; 39 | ``` 40 | 41 | ### 3. Middleware for Age Validation 42 | - Inside the `middlewares` directory, modify the `checkAge.js` middleware. 43 | - It should check if an `age` parameter is provided in the `/about/:age?` route. 44 | - If `age` is missing, respond with: `"Please provide your age."` 45 | - If `age` is less than 18, respond with: `"You are not allowed to enter this page."` 46 | - If `age` is 18 or above, pass control to the next middleware or route handler. 47 | 48 | ```javascript 49 | // checkAge.js 50 | const checkAge = (req, res, next) => { 51 | const age = req.params.age; 52 | if (!age) { 53 | return res.send("Please provide your age."); 54 | } 55 | if (age < 18) { 56 | return res.send("You are not allowed to enter this page."); 57 | } 58 | next(); 59 | }; 60 | 61 | module.exports = checkAge; 62 | ``` 63 | 64 | ### 4. Routes in `index.js` 65 | - Add the middleware to the respective routes in your `index.js` file. 66 | - Use `validateName.js` for the `/contact` route to check for a `name` query string. 67 | - Use `checkAge.js` for the `/about/:age?` route to check for the age parameter. 68 | 69 | ```javascript 70 | // index.js 71 | const express = require('express'); 72 | const app = express(); 73 | 74 | // Middleware and static files 75 | app.use(express.static('public')); 76 | const validateName = require('./middlewares/validateName'); 77 | const checkAge = require('./middlewares/checkAge'); 78 | 79 | // Routes 80 | app.get('/', (req, res) => { 81 | res.send('Hello World!'); 82 | }); 83 | 84 | app.get('/about/:age?', checkAge, (req, res) => { 85 | res.send('About Us'); 86 | }); 87 | 88 | app.use('/contact', validateName); 89 | app.get('/contact', (req, res) => { 90 | res.send(`Contact Us, ${req.query.name}`); 91 | }); 92 | 93 | // Start server 94 | app.listen(3000, () => { 95 | console.log('Server is running on port 3000'); 96 | }); 97 | ``` 98 | 99 | ### 5. Running the Application 100 | - To run the application, install the dependencies using `npm install` and start the server with `node index.js`. 101 | - The server will run on `http://localhost:3000`. 102 | 103 | ### 6. Testing the Application 104 | - Test the `/contact` route with and without the `name` query parameter. Example: `http://localhost:3000/contact?name=John`. 105 | - Test the `/about/:age?` route with different age values. Example: `http://localhost:3000/about/25` or `http://localhost:3000/about/15`. 106 | 107 | ## Conclusion 108 | By completing this assignment, you will have a better understanding of middleware, route parameters, and query handling in Express.js. --------------------------------------------------------------------------------