Q1. What is app.all() in Express?
app.all() is a special routing method that handles all HTTP methods (GET, POST, PUT, DELETE, etc.) for a specific path. It's useful for mounting middleware that should run regardless of the HTTP method, like authentication or logging for a particular route.
Q2. What are Express routers and why use them?
Express.Router() creates modular, mountable route handlers. Instead of defining all routes on the main app, you can create separate router instances for different sections (e.g., usersRouter, productsRouter). This helps organize code and makes it more maintainable.
Q3. How do you create and use a router?
Create a router file: const router = express.Router(); router.get('/', (req, res) => {...}); module.exports = router;. Then in your main app: const userRouter = require('./routes/users'); app.use('/users', userRouter); This prefixes all routes in that router with '/users'.
Q4. Can you nest routers?
Yes, routers can use other routers with router.use(). For example, an admin router can mount a users router. This allows creating deeply nested, modular route structures. Each router acts as a mini-application with its own middleware and routes.
Q5. What is route chaining in Express?
Route chaining allows defining multiple HTTP methods on the same route path using app.route(). Example: app.route('/book').get((req, res) => {...}).post((req, res) => {...}). This is cleaner than repeating the path for each method and helps with code organization.
