0
0
Expressframework~10 mins

Virtual path prefixes in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mount a router on the '/api' virtual path prefix.

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

app.use([1], router);
Drag options to blanks, or click blank then click option'
A'/api'
B'api'
C'/router'
D'/'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'api' without the leading slash
Using '/' which mounts at root
2fill in blank
medium

Complete the code to define a GET route on the router for '/users'.

Express
const router = require('express').Router();

router.[1]('/users', (req, res) => {
  res.send('User list');
});
Drag options to blanks, or click blank then click option'
Adelete
Bpost
Cput
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get'
Using 'put' or 'delete' for fetching data
3fill in blank
hard

Fix the error in mounting the router so it uses the virtual path prefix '/api'.

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

app.use([1], router);
Drag options to blanks, or click blank then click option'
A'/api'
Brouter
C'api'
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of arguments
Using 'api' without slash
4fill in blank
hard

Fill both blanks to create a router mounted on '/admin' that responds to GET requests at '/dashboard'.

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

router.[1]('/dashboard', (req, res) => {
  res.send('Admin Dashboard');
});

app.use([2], router);
Drag options to blanks, or click blank then click option'
Aget
Bpost
C'/admin'
D'/dashboard'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get'
Mounting on wrong path like '/dashboard'
5fill in blank
hard

Fill all three blanks to create a router mounted on '/shop' with a POST route at '/order' that sends 'Order received'.

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

router.[1]([2], (req, res) => {
  res.send('Order received');
});

app.use([3], router);
Drag options to blanks, or click blank then click option'
Apost
B'/order'
C'/shop'
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'post'
Wrong route or mount paths