Performance: Virtual path prefixes
This affects server routing speed and resource loading efficiency by managing how URL paths map to middleware or static files.
app.use('/static', express.static('public'))
app.use(express.static('public'))| Pattern | Routing Checks | File System Access | Response Time Impact | Verdict |
|---|---|---|---|---|
| No virtual prefix (app.use(express.static('public'))) | High (all requests) | High (all requests) | Slower due to unnecessary checks | [X] Bad |
| With virtual prefix (app.use('/static', express.static('public'))) | Low (filtered requests only) | Low (filtered requests only) | Faster due to targeted checks | [OK] Good |
app.use('/prefix', router)?app.use('/prefix', router) doesapp.use(path, middleware) to mount routers or middleware.app.use is the correct method; others like app.route, app.mount, and app.router are invalid or incorrect here.const express = require('express');
const app = express();
const adminRouter = express.Router();
adminRouter.get('/', (req, res) => res.send('Hello from admin'));
app.use('/admin', adminRouter);
app.listen(3000);const express = require('express');
const app = express();
const userRouter = express.Router();
userRouter.get('/profile', (req, res) => res.send('User Profile'));
app.use(userRouter, '/user');
app.listen(3000);app.use(path, router), but here it is reversed.app.use('/user', userRouter) to mount the router at '/user'.adminRouter mounted at '/admin' and userRouter mounted at '/user'. Both routers have a route for '/' that sends different greetings. Which code correctly sets this up so visiting '/admin' and '/user' shows the correct messages?app.use(path, router) to mount routers at virtual path prefixes.app.use('/admin', adminRouter); and app.use('/user', userRouter);. Distractors use invalid methods like app.route() or app.get() with routers, or reverse the path and router order in app.use().Pass the test and this pattern moves unknown