Express JS Assignment- 2

Basic Questions

  1. Use express.static() to serve files from a folder named public.
  2. Place an index.html file in the public folder and serve it using Express.
  3. Add a CSS file in public/styles.css and link it in your index.html.
  4. Demonstrate how to change the default static directory name from public to assets.
  5. 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.
  6. Enable basic caching for static files using maxAge option in express.static().
  7. Add gzip compression for static files using the compression middleware.
  8. Install and configure the debug module in your Express app.
  9. Log request method and URL using the debug module.
  10. Create a simple nested route /admin/dashboard.
  11. Create a conditional route that only works if a query parameter mode=dev is present.
  12. Use express.Router() to organize /users routes into a separate file.
  13. Add another route group /posts using express.Router().
  14. Create a custom Express middleware that logs “Middleware executed” on every request before sending the response.
  15. Use built-in middleware express.json() and express.urlencoded().
  16. Install a third-party middleware morgan and log requests.
  17. Write a custom middleware that prints “Request Received”.
  18. Create a middleware chain of two middlewares that run before a route.
  19. Demonstrate error handling middleware using app.use((err, req, res, next) => {…}).
  20. Use multer to handle single file upload with field name profilePic.

Intermediate Questions

  1. Demonstrate serving multiple static folders (public and uploads).
  2. 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.
  3. Configure caching to serve static files with cache invalidation on update.
  4. Use compression with conditional logic (enable only for production).
  5. Use debug namespaces (e.g., app:server, app:db) for better logs.
  6. Monitor both incoming requests and outgoing responses with debug.
  7. Create nested routes /products/:id/reviews.
  8. Implement conditional routing: /admin only accessible if role=admin.
  9. Organize routes into routes/users.js and routes/posts.js.
  10. Group routes under /api/v1 using express.Router().
  11. Demonstrate middleware execution order with 3 middlewares.
  12. Write a custom logging middleware that logs time taken for each request.
  13. Write a request transformation middleware that adds a timestamp property to req.body.
  14. Write a middleware that checks for Authorization header.
  15. Implement global error handling middleware.
  16. Use express-validator to validate that email is valid in /register.
  17. Add validation for password length ≥ 8 in /register.
  18. Configure multer to accept multiple file uploads (field name photos).
  19. Validate file type in multer to allow only .jpg and .png.
  20. Configure multer to save uploaded files in a folder uploads/images.

Advanced Questions

  1. Create an Express app serving static files with cache-control and gzip compression enabled.
  2. Implement debugging with debug module that logs only failed requests.
  3. Implement advanced nested routes: /users/:id/orders/:orderId/items/:itemId.
  4. Create a conditional route /beta that only works if NODE_ENV=development.
  5. Write a custom error-handling middleware that logs errors to a file before sending response.
  6. Implement JWT authentication middleware that protects /dashboard route.
  7. Implement session-based authentication middleware for /profile.
  8. Use express-validator to validate registration form: name (required), email (valid), password (min 8 chars).
  9. Use multer to upload a PDF file, validate size (max 2MB), and reject if invalid.
  10. Integrate multer with AWS S3 to store uploaded files in cloud storage.