0
0
Expressframework~10 mins

GET route handling 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 GET route for the path '/hello'.

Express
app.[1]('/hello', (req, res) => {
  res.send('Hello World!');
});
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'post' instead of 'get' for a GET route.
Using 'put' or 'delete' which are for other HTTP methods.
2fill in blank
medium

Complete the code to send a JSON response with a message in the GET route.

Express
app.get('/json', (req, res) => {
  res.[1]({ message: 'Hello JSON' });
});
Drag options to blanks, or click blank then click option'
Aredirect
BsendFile
Crender
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using sendFile which sends a file, not JSON.
Using render which is for templates.
3fill in blank
hard

Fix the error in the GET route that tries to access a URL parameter.

Express
app.get('/user/:id', (req, res) => {
  const userId = req.params.[1];
  res.send(`User ID is ${userId}`);
});
Drag options to blanks, or click blank then click option'
Auserid
Bid
Cparam
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the URL parameter.
Trying to access req.param instead of req.params.
4fill in blank
hard

Fill both blanks to create a GET route that sends a status code and a JSON message.

Express
app.[1]('/status', (req, res) => {
  res.status([2]).json({ message: 'All good' });
});
Drag options to blanks, or click blank then click option'
Aget
Bpost
C404
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST method instead of GET.
Using 404 status code which means not found.
5fill in blank
hard

Fill all three blanks to create a GET route that reads a query parameter and sends it back in JSON.

Express
app.[1]('/search', (req, res) => {
  const term = req.query.[2];
  res.[3]({ searchTerm: term });
});
Drag options to blanks, or click blank then click option'
Aget
Bquery
Cjson
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using params instead of query for query parameters.
Using send instead of json for JSON response.