0
0
Rest APIprogramming~10 mins

Resource-based design thinking in Rest API - Interactive Code Practice

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

Complete the code to define a REST API endpoint that returns a list of resources.

Rest API
app.get('/resources', (req, res) => {
  res.[1](resources);
});
Drag options to blanks, or click blank then click option'
Asend
Bfetch
Cpost
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch instead of send to respond.
Using post or put for a GET route.
2fill in blank
medium

Complete the code to extract the resource ID from the URL parameters.

Rest API
app.get('/resources/:id', (req, res) => {
  const resourceId = req.[1].id;
  res.send(findResourceById(resourceId));
});
Drag options to blanks, or click blank then click option'
Abody
Bquery
Cparams
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body for URL params.
Using req.query instead of req.params.
3fill in blank
hard

Fix the error in the code to correctly create a new resource from the request body.

Rest API
app.post('/resources', (req, res) => {
  const newResource = req.[1];
  resources.push(newResource);
  res.status(201).send(newResource);
});
Drag options to blanks, or click blank then click option'
Aparams
Bbody
Cquery
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params or req.query for POST data.
Forgetting to parse JSON body.
4fill in blank
hard

Fill both blanks to update a resource by ID and send the updated resource.

Rest API
app.put('/resources/:id', (req, res) => {
  const id = req.[1].id;
  const updatedData = req.[2];
  const index = resources.findIndex(r => r.id === id);
  if (index !== -1) {
    resources[index] = {...resources[index], ...updatedData};
    res.send(resources[index]);
  } else {
    res.status(404).send({error: 'Resource not found'});
  }
});
Drag options to blanks, or click blank then click option'
Aparams
Bbody
Cquery
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up params and body.
Using query instead of params for ID.
5fill in blank
hard

Fill all three blanks to delete a resource by ID and send a confirmation message.

Rest API
app.delete('/resources/:id', (req, res) => {
  const id = req.[1].id;
  const index = resources.findIndex(r => r.id === id);
  if (index !== -1) {
    resources.[2](index, 1);
    res.[3]({ message: 'Resource deleted' });
  } else {
    res.status(404).send({ error: 'Resource not found' });
  }
});
Drag options to blanks, or click blank then click option'
Aparams
Bsplice
Csend
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of splice.
Using req.body instead of req.params for ID.
Using res.status without send.