0
0
Expressframework~10 mins

Why REST principles matter in Express - Test Your Understanding

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

Complete the code to create a basic Express server that listens on port 3000.

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

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen([1], () => {
  console.log('Server is running');
});
Drag options to blanks, or click blank then click option'
A80
B3000
C5000
D8080
Attempts:
3 left
💡 Hint
Common Mistakes
Using a port number that is already in use or reserved.
Forgetting to pass a number as the port.
2fill in blank
medium

Complete the code to parse incoming JSON request bodies in Express.

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

app.use([1]());

app.post('/data', (req, res) => {
  res.json(req.body);
});
Drag options to blanks, or click blank then click option'
Aexpress.static
Bexpress.urlencoded
Cexpress.json
Dexpress.raw
Attempts:
3 left
💡 Hint
Common Mistakes
Using express.urlencoded() which parses URL-encoded data, not JSON.
Forgetting to use any body parsing middleware.
3fill in blank
hard

Fix the error in the route handler to correctly send a 404 status when a resource is not found.

Express
app.get('/item/:id', (req, res) => {
  const item = database.find(i => i.id === req.params.id);
  if (!item) {
    res.status([1]).send('Not Found');
  } else {
    res.json(item);
  }
});
Drag options to blanks, or click blank then click option'
A404
B200
C500
D301
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means success even when the item is missing.
Using 500 which means server error, not client error.
4fill in blank
hard

Fill both blanks to create a RESTful route that updates an item by ID using the correct HTTP method and URL pattern.

Express
app.[1]('/items/[2]', (req, res) => {
  // update item logic here
  res.send('Item updated');
});
Drag options to blanks, or click blank then click option'
Aput
Bpost
Cpatch
D:id
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST which is for creating resources.
Omitting the ID parameter in the URL.
5fill in blank
hard

Fill all three blanks to create a RESTful Express route that deletes an item by ID and sends a 204 status code.

Express
app.[1]('/items/[2]', (req, res) => {
  // delete item logic
  res.status([3]).send();
});
Drag options to blanks, or click blank then click option'
Adelete
B:itemId
C204
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 status code which usually includes a response body.
Using POST or PUT instead of DELETE.