Express JS Assignment- 2
Basic Questions
- Use express.static() to serve files from a folder named public.
- Place an index.html file in the public folder and serve it using Express.
- Add a CSS file in public/styles.css and link it in your index.html.
- Demonstrate how to change the default static directory name from public to assets.
- Configure express.static() with a cache setting using maxAge, then build a small test by serving an image or CSS file. Open browser DevTools → show from second refresh that resource is served from cache.
- Enable basic caching for static files using maxAge option in express.static().
- Add gzip compression for static files using the compression middleware.
- Install and configure the debug module in your Express app.
- Log request method and URL using the debug module.
- Create a simple nested route /admin/dashboard.
- Create a conditional route that only works if a query parameter mode=dev is present.
- Use express.Router() to organize /users routes into a separate file.
- Add another route group /posts using express.Router().
- Create a custom Express middleware that logs “Middleware executed” on every request before sending the response.
- Use built-in middleware express.json() and express.urlencoded().
- Install a third-party middleware morgan and log requests.
- Write a custom middleware that prints “Request Received”.
- Create a middleware chain of two middlewares that run before a route.
- Demonstrate error handling middleware using app.use((err, req, res, next) => {…}).
- Use multer to handle single file upload with field name profilePic.
Intermediate Questions
- Demonstrate serving multiple static folders (public and uploads).
- Serve a static JavaScript file from Express and in the same HTML page also load the same library from a CDN URL, then compare their responses in browser DevTools.
- Configure caching to serve static files with cache invalidation on update.
- Use compression with conditional logic (enable only for production).
- Use debug namespaces (e.g., app:server, app:db) for better logs.
- Monitor both incoming requests and outgoing responses with debug.
- Create nested routes /products/:id/reviews.
- Implement conditional routing: /admin only accessible if role=admin.
- Organize routes into routes/users.js and routes/posts.js.
- Group routes under /api/v1 using express.Router().
- Demonstrate middleware execution order with 3 middlewares.
- Write a custom logging middleware that logs time taken for each request.
- Write a request transformation middleware that adds a timestamp property to req.body.
- Write a middleware that checks for Authorization header.
- Implement global error handling middleware.
- Use express-validator to validate that email is valid in /register.
- Add validation for password length ≥ 8 in /register.
- Configure multer to accept multiple file uploads (field name photos).
- Validate file type in multer to allow only .jpg and .png.
- Configure multer to save uploaded files in a folder uploads/images.
Advanced Questions
- Create an Express app serving static files with cache-control and gzip compression enabled.
- Implement debugging with debug module that logs only failed requests.
- Implement advanced nested routes: /users/:id/orders/:orderId/items/:itemId.
- Create a conditional route /beta that only works if NODE_ENV=development.
- Write a custom error-handling middleware that logs errors to a file before sending response.
- Implement JWT authentication middleware that protects /dashboard route.
- Implement session-based authentication middleware for /profile.
- Use express-validator to validate registration form: name (required), email (valid), password (min 8 chars).
- Use multer to upload a PDF file, validate size (max 2MB), and reject if invalid.
- Integrate multer with AWS S3 to store uploaded files in cloud storage.