0
0
Rest APIprogramming~10 mins

Why advanced patterns solve real problems in Rest API - Test Your Understanding

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

Complete the code to define a simple GET endpoint that returns a welcome message.

Rest API
app.get('/welcome', (req, res) => {
  res.[1]('Welcome to the API!');
});
Drag options to blanks, or click blank then click option'
Ajson
Bsend
Cwrite
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.json instead of res.send
Using res.status without sending a response
2fill in blank
medium

Complete the code to parse JSON data from a POST request body.

Rest API
app.use([1]());
Drag options to blanks, or click blank then click option'
Aexpress.json
Bexpress.urlencoded
CbodyParser.text
DbodyParser.raw
Attempts:
3 left
💡 Hint
Common Mistakes
Using urlencoded middleware for JSON data
Using deprecated bodyParser modules
3fill in blank
hard

Fix the error in the route handler to correctly extract the user ID from the URL parameter.

Rest API
app.get('/user/:id', (req, res) => {
  const userId = req.[1].id;
  res.send(`User ID is ${userId}`);
});
Drag options to blanks, or click blank then click option'
Aparams
Bbody
Cquery
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body instead of req.params
Using req.query for URL parameters
4fill in blank
hard

Fill both blanks to create a middleware that logs the HTTP method and URL of each request.

Rest API
app.use((req, res, next) => {
  console.log(req.[1] + ' ' + req.[2]);
  next();
});
Drag options to blanks, or click blank then click option'
Amethod
Burl
Cpath
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.path instead of req.url
Logging req.query which is the query parameters
5fill in blank
hard

Fill all three blanks to create a route that responds with JSON containing the user's name and age if age is greater than 18.

Rest API
app.post('/check', (req, res) => {
  const { name, age } = req.body;
  if (age [1] 18) {
    res.[2]({ [3] });
  } else {
    res.status(403).send('Forbidden');
  }
});
Drag options to blanks, or click blank then click option'
A>
Bjson
Cname, age
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of >
Using res.send instead of res.json
Not sending the correct data object