0
0
Expressframework~10 mins

Resource-based route organization 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 create a new Express router.

Express
const express = require('express');
const router = express.[1]();
Drag options to blanks, or click blank then click option'
ARouter
Broute
Capp
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.route() instead of express.Router()
Using express.app() which does not exist
2fill in blank
medium

Complete the code to define a GET route for '/users' that sends a JSON response.

Express
router.[1]('/users', (req, res) => {
  res.json({ message: 'List of users' });
});
Drag options to blanks, or click blank then click option'
Aget
Bput
Cdelete
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get
Using put or delete which are for other actions
3fill in blank
hard

Fix the error in the route path to correctly capture a user ID parameter.

Express
router.get('/users/[1]', (req, res) => {
  const userId = req.params.id;
  res.send(`User ID: ${userId}`);
});
Drag options to blanks, or click blank then click option'
A:userId
B:id
CuserId
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the colon
Using a different parameter name than accessed in code
4fill in blank
hard

Fill both blanks to export the router and import it in the main app file.

Express
// In routes/users.js
const express = require('express');
const router = express.Router();
// ... define routes ...
module.exports = [1];

// In app.js
const express = require('express');
const app = express();
const usersRouter = require('./routes/users');
app.use('/users', [2]);
Drag options to blanks, or click blank then click option'
Arouter
Bapp
CusersRouter
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Exporting 'app' instead of 'router'
Using 'router' directly in app.use instead of 'usersRouter'
5fill in blank
hard

Fill all three blanks to create a resource-based route for posts with GET, POST, and DELETE methods.

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

router.[1]('/posts', (req, res) => {
  res.send('Get all posts');
});

router.[2]('/posts', (req, res) => {
  res.send('Create a post');
});

router.[3]('/posts/:id', (req, res) => {
  res.send(`Delete post ${req.params.id}`);
});
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cdelete
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing HTTP methods like using PUT instead of POST
Using GET for deleting resources