0
0
Expressframework~10 mins

req.query for query strings 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 access the query string parameter named 'name'.

Express
app.get('/hello', (req, res) => {
  const userName = req.[1].name;
  res.send(`Hello, ${userName}!`);
});
Drag options to blanks, or click blank then click option'
Aquery
Bbody
Cparams
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params instead of req.query
Trying to get query parameters from req.body
2fill in blank
medium

Complete the code to send back the 'age' query parameter as a number.

Express
app.get('/age', (req, res) => {
  const age = Number(req.[1].age);
  res.send(`You are ${age} years old.`);
});
Drag options to blanks, or click blank then click option'
Abody
Bparams
Ccookies
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting the query string to a number
Using req.params instead of req.query
3fill in blank
hard

Fix the error in accessing the 'search' query parameter.

Express
app.get('/search', (req, res) => {
  const term = req.[1].search;
  res.send(`Searching for: ${term}`);
});
Drag options to blanks, or click blank then click option'
Aquery
Bparams
Cbody
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params or req.body instead of req.query
Trying to access query parameters from req.session
4fill in blank
hard

Fill both blanks to create a response showing 'category' and 'page' from query strings.

Express
app.get('/items', (req, res) => {
  const category = req.[1].category;
  const page = Number(req.[2].page);
  res.send(`Category: ${category}, Page: ${page}`);
});
Drag options to blanks, or click blank then click option'
Aquery
Bparams
Cbody
Dcookies
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing req.params and req.query
Not converting page to a number
5fill in blank
hard

Fill all three blanks to create a filtered list using 'type', 'sort', and 'limit' query parameters.

Express
app.get('/filter', (req, res) => {
  const type = req.[1].type;
  const sort = req.[2].sort;
  const limit = Number(req.[3].limit);
  res.send(`Filter: type=${type}, sort=${sort}, limit=${limit}`);
});
Drag options to blanks, or click blank then click option'
Aparams
Bquery
Cbody
Dcookies
Attempts:
3 left
💡 Hint
Common Mistakes
Using different request properties for each parameter
Forgetting to convert 'limit' to a number