This serves static files from the 'public' folder under the '/static' URL path.
Express
app.use('/static', express.static('public'));
Sample Program
This example creates a router with a route '/info'. The router is mounted at '/docs'. So visiting '/docs/info' shows the router response. The home page is at '/'.
D. Router should be created with express() not express.Router()
Solution
Step 1: Check the app.use argument order
Correct syntax is app.use(path, router), but here it is reversed.
Step 2: Identify the correct argument order
It should be app.use('/user', userRouter) to mount the router at '/user'.
Final Answer:
The order of arguments in app.use is incorrect -> Option A
Quick Check:
app.use(path, router) order matters [OK]
Hint: app.use(path, router) not app.use(router, path) [OK]
Common Mistakes:
Swapping router and path arguments
Thinking router path is invalid
Forgetting to call app.listen
5. You want to create an Express app with two routers: 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?
hard
A. app.use('/admin', adminRouter);
app.use('/user', userRouter);
B. app.use(adminRouter, '/admin');
app.use(userRouter, '/user');
C. app.route('/admin', adminRouter);
app.route('/user', userRouter);
D. app.get('/admin', adminRouter);
app.get('/user', userRouter);
Solution
Step 1: Recall correct syntax for mounting routers with prefixes
Use app.use(path, router) to mount routers at virtual path prefixes.
Step 2: Evaluate the options
The correct code uses 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().
Final Answer:
app.use('/admin', adminRouter);
app.use('/user', userRouter); -> Option A
Quick Check:
Mount routers with app.use(path, router) [OK]
Hint: Mount routers with app.use(path, router) for multiple prefixes [OK]
Common Mistakes:
Swapping router and path in app.use
Using app.route or app.get to mount routers
Not mounting routers at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.