Given the Express app setup below with routes separated into files, what will the server respond when a client requests /users?
const express = require('express'); const app = express(); const usersRouter = require('./routes/users'); app.use('/users', usersRouter); // routes/users.js const express = require('express'); const router = express.Router(); router.get('/', (req, res) => { res.send('User list'); }); module.exports = router;
Check how the router handles the / path inside the /users prefix.
The usersRouter handles requests to / relative to /users. So a GET request to /users matches router.get('/', ...) and responds with 'User list'.
Choose the correct way to export an Express router from a file named products.js.
const express = require('express'); const router = express.Router(); router.get('/', (req, res) => { res.send('Product list'); }); // What is the correct export statement here?
Remember how CommonJS modules export values.
In CommonJS, module.exports = router; correctly exports the router object. exports = router; only reassigns the local variable and does not change the exported object. export default is ES module syntax and not valid here. module.export is a typo.
Consider this Express app setup:
const express = require('express');
const app = express();
const apiRouter = require('./routes/api');
app.use('/api', apiRouter);
// routes/api.js
const express = require('express');
const router = express.Router();
router.get('/data', (req, res) => {
// missing res.send or res.end
});
module.exports = router;What happens when a client requests /api/data?
Check if the route handler sends a response.
The route handler does not call res.send() or res.end(), so the request stays open and the client waits indefinitely until timeout.
Why do developers separate routes into different files when building Express applications?
Think about code organization and teamwork.
Separating routes into files helps keep code organized, easier to read, and maintain. It also allows multiple developers to work on different routes without conflicts. It does not directly affect performance or middleware usage.
Given the following Express app and route files, what will be the output when a client requests /api/users/profile?
const express = require('express'); const app = express(); const apiRouter = require('./routes/api'); app.use('/api', apiRouter); // routes/api.js const express = require('express'); const router = express.Router(); const usersRouter = require('./users'); router.use('/users', (req, res, next) => { req.customData = 'middleware data'; next(); }); router.use('/users', usersRouter); module.exports = router; // routes/users.js const express = require('express'); const router = express.Router(); router.get('/profile', (req, res) => { res.send(req.customData); }); module.exports = router;
Consider how middleware modifies req and how nested routers handle requests.
The middleware on /users adds req.customData. Then the nested usersRouter handles /profile and sends req.customData. So the response is 'middleware data'.