Bird
Raised Fist0
Expressframework~20 mins

Namespaces for separation in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Express Namespaces Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing routes with namespaces?

Consider this Express app code that uses namespaces (routers) to separate routes:

const express = require('express');
const app = express();

const apiRouter = express.Router();
apiRouter.get('/users', (req, res) => res.send('API Users'));

const adminRouter = express.Router();
adminRouter.get('/users', (req, res) => res.send('Admin Users'));

app.use('/api', apiRouter);
app.use('/admin', adminRouter);

app.listen(3000);

What will be the response when a client requests GET /admin/users?

Express
const express = require('express');
const app = express();

const apiRouter = express.Router();
apiRouter.get('/users', (req, res) => res.send('API Users'));

const adminRouter = express.Router();
adminRouter.get('/users', (req, res) => res.send('Admin Users'));

app.use('/api', apiRouter);
app.use('/admin', adminRouter);

app.listen(3000);
AThe response is 'Admin Users'.
BThe response is 'API Users'.
C404 Not Found error.
D500 Internal Server Error.
Attempts:
2 left
💡 Hint

Think about how the app.use prefixes the routes in each router.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a namespace router in Express?

Which code snippet correctly creates a router namespace for /blog in Express?

Aconst blogRouter = express.Router; app.use('/blog', blogRouter);
Bconst blogRouter = new express.Router(); app.use('/blog', blogRouter);
Cconst blogRouter = express(); app.use('/blog', blogRouter);
Dconst blogRouter = express.Router(); app.use('/blog', blogRouter);
Attempts:
2 left
💡 Hint

Remember how to create a router instance in Express.

🔧 Debug
advanced
2:00remaining
Why does this namespaced route not respond as expected?

Given this Express code:

const express = require('express');
const app = express();
const userRouter = express.Router();

userRouter.get('/profile', (req, res) => res.send('User Profile'));

app.use('/user', userRouter);

app.listen(3000);

When requesting GET /profile, the server responds with 404. Why?

Express
const express = require('express');
const app = express();
const userRouter = express.Router();

userRouter.get('/profile', (req, res) => res.send('User Profile'));

app.use('/user', userRouter);

app.listen(3000);
ABecause the route is mounted under '/user', so '/profile' is not matched; the correct path is '/user/profile'.
BBecause the route handler is missing a next() call.
CBecause the router was not attached with app.use().
DBecause the server is not listening on port 3000.
Attempts:
2 left
💡 Hint

Check the full path the route is mounted on.

❓ state_output
advanced
2:00remaining
What is the output of nested namespaces with middleware?

Consider this Express app:

const express = require('express');
const app = express();

const apiRouter = express.Router();
const v1Router = express.Router();

v1Router.use((req, res, next) => {
  req.version = 'v1';
  next();
});

v1Router.get('/data', (req, res) => {
  res.send(`API version: ${req.version}`);
});

apiRouter.use('/v1', v1Router);
app.use('/api', apiRouter);

app.listen(3000);

What will be the response body when a client requests GET /api/v1/data?

Express
const express = require('express');
const app = express();

const apiRouter = express.Router();
const v1Router = express.Router();

v1Router.use((req, res, next) => {
  req.version = 'v1';
  next();
});

v1Router.get('/data', (req, res) => {
  res.send(`API version: ${req.version}`);
});

apiRouter.use('/v1', v1Router);
app.use('/api', apiRouter);

app.listen(3000);
A404 Not Found
BAPI version: undefined
CAPI version: v1
D500 Internal Server Error
Attempts:
2 left
💡 Hint

Look at how middleware sets req.version before the route handler.

🧠 Conceptual
expert
2:00remaining
Which statement about Express namespaces (routers) is true?

Choose the correct statement about using namespaces (routers) in Express:

ARouters cannot have middleware; middleware must be applied only on the main app instance.
BRouters can be nested and each can have its own middleware and routes, allowing modular route separation.
CRoutes defined in routers lose access to the original request object properties.
DRouters automatically merge query parameters from parent routers without extra configuration.
Attempts:
2 left
💡 Hint

Think about how routers help organize routes and middleware.

Practice

(1/5)
1. What is the main purpose of using namespaces in Express routing?
easy
A. To group related routes under a common path prefix
B. To improve database query speed
C. To style HTML elements
D. To manage user sessions

Solution

  1. Step 1: Understand what namespaces do in Express

    Namespaces help organize routes by grouping them under a shared path prefix.
  2. Step 2: Compare with other options

    Options B, C, and D relate to other concerns like database, styling, or sessions, not routing namespaces.
  3. Final Answer:

    To group related routes under a common path prefix -> Option A
  4. Quick Check:

    Namespaces = Group routes [OK]
Hint: Namespaces group routes by path prefix [OK]
Common Mistakes:
  • Confusing namespaces with database or styling concepts
  • Thinking namespaces manage sessions
  • Believing namespaces affect frontend styling
2. Which of the following is the correct way to attach a router to an Express app under the namespace '/api'?
easy
A. app.router('/api', router);
B. app.attach('/api', router);
C. app.route('/api', router);
D. app.use('/api', router);

Solution

  1. Step 1: Recall Express method to use middleware or routers

    Express uses app.use(path, router) to mount routers under a path.
  2. Step 2: Check other options for validity

    Options A, B, and C are not valid Express methods for attaching routers.
  3. Final Answer:

    app.use('/api', router); -> Option D
  4. Quick Check:

    Attach router with app.use() [OK]
Hint: Use app.use('/path', router) to attach routers [OK]
Common Mistakes:
  • Using app.router instead of app.use
  • Confusing app.route with router attachment
  • Trying to use non-existent methods like app.attach
3. Given the code below, what will be the full URL path to access the route handler?
const express = require('express');
const app = express();
const userRouter = express.Router();

userRouter.get('/profile', (req, res) => res.send('User Profile'));
app.use('/user', userRouter);
medium
A. /user/profile
B. /profile
C. /user
D. /profile/user

Solution

  1. Step 1: Identify the namespace prefix

    The router is attached with app.use('/user', userRouter), so '/user' is the base path.
  2. Step 2: Combine router path with namespace

    The route inside the router is '/profile', so full path is '/user/profile'.
  3. Final Answer:

    /user/profile -> Option A
  4. Quick Check:

    Namespace + route = full path [OK]
Hint: Full path = namespace + router path [OK]
Common Mistakes:
  • Ignoring the namespace prefix
  • Using only the router path without prefix
  • Mixing order of path segments
4. Identify the error in the following Express code that tries to create a namespace:
const express = require('express');
const app = express();
const adminRouter = express.Router();

adminRouter.get('/dashboard', (req, res) => res.send('Admin Dashboard'));
app.use(adminRouter, '/admin');
medium
A. Missing app.listen call
B. adminRouter.get should be adminRouter.post
C. Router should be attached with app.use('/admin', adminRouter);
D. Cannot use express.Router() for namespaces

Solution

  1. Step 1: Check app.use syntax

    The correct syntax is app.use('/path', router), but here arguments are reversed.
  2. Step 2: Confirm other options

    adminRouter.get should be adminRouter.post is incorrect because GET is valid; C is unrelated to namespace error; D is false because express.Router() is used for namespaces.
  3. Final Answer:

    Router should be attached with app.use('/admin', adminRouter); -> Option C
  4. Quick Check:

    app.use(path, router) order matters [OK]
Hint: app.use(path, router) order is important [OK]
Common Mistakes:
  • Swapping arguments in app.use
  • Thinking HTTP method must be POST
  • Confusing missing listen with routing error
5. You want to create two separate namespaces in Express: '/api' for API routes and '/admin' for admin routes. Which code snippet correctly sets this up?
hard
A. const apiRouter = express.Router(); const adminRouter = express.Router(); app.route('/api', apiRouter); app.route('/admin', adminRouter);
B. const apiRouter = express.Router(); const adminRouter = express.Router(); app.use('/api', apiRouter); app.use('/admin', adminRouter);
C. const apiRouter = express.Router(); const adminRouter = express.Router(); app.use(apiRouter, '/api'); app.use(adminRouter, '/admin');
D. const apiRouter = express.Router(); const adminRouter = express.Router(); app.attach('/api', apiRouter); app.attach('/admin', adminRouter);

Solution

  1. Step 1: Create routers for each namespace

    Both apiRouter and adminRouter are created using express.Router().
  2. Step 2: Attach routers with correct syntax

    Use app.use('/path', router) to mount routers under '/api' and '/admin'.
  3. Step 3: Verify other options

    Options A, B, and D use incorrect methods or argument orders.
  4. Final Answer:

    const apiRouter = express.Router(); const adminRouter = express.Router(); app.use('/api', apiRouter); app.use('/admin', adminRouter); -> Option B
  5. Quick Check:

    Separate namespaces need separate routers and app.use() [OK]
Hint: Use separate routers and app.use('/path', router) [OK]
Common Mistakes:
  • Reversing arguments in app.use
  • Using app.route or app.attach incorrectly
  • Trying to attach routers without namespaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard