0
0
Node.jsframework~10 mins

Request object properties in Node.js - 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 HTTP method from the request object.

Node.js
const method = req.[1];
Drag options to blanks, or click blank then click option'
Amethod
Burl
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Trying to access req.body for the method
Using req.headers which holds headers, not method
2fill in blank
medium

Complete the code to get the URL path from the request object.

Node.js
const path = req.[1];
Drag options to blanks, or click blank then click option'
Amethod
Bpath
Curl
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.path which may not exist in plain Node.js
Using req.method which is the HTTP method
Using req.query which holds parsed query parameters
3fill in blank
hard

Fix the error in accessing the request headers property.

Node.js
const headers = req.[1];
Drag options to blanks, or click blank then click option'
Aheader
Bheaders
Chead
DheaderList
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'header' instead of 'headers'
Trying to access 'head' or 'headerList' which do not exist
4fill in blank
hard

Fill both blanks to extract the query string parameters from the request URL.

Node.js
const url = require('url');
const queryObject = url.[1](req.[2], true).query;
Drag options to blanks, or click blank then click option'
Aparse
Burl
Cmethod
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.method instead of req.url
Using url.method or url.headers which do not exist
5fill in blank
hard

Fill all three blanks to read JSON data sent in the request body using streams.

Node.js
let body = '';
req.on('[1]', chunk => {
  body += chunk.toString();
});
req.on('[2]', () => {
  const data = JSON.[3](body);
  console.log(data);
});
Drag options to blanks, or click blank then click option'
Adata
Bend
Cparse
Dstringify
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' event to collect chunks instead of 'data'
Using JSON.stringify instead of JSON.parse
Mixing event names or JSON methods