0
0
Expressframework~10 mins

Why logging matters in production in Express - Test Your Understanding

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

Complete the code to log a message when the server starts.

Express
app.listen(3000, () => { console.log([1]); });
Drag options to blanks, or click blank then click option'
A"Server is running on port 3000"
B3000
Capp.listen
Dconsole.log
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the port number directly instead of a message.
Using app.listen inside console.log.
2fill in blank
medium

Complete the code to log the HTTP method of incoming requests.

Express
app.use((req, res, next) => { console.log(req.[1]); next(); });
Drag options to blanks, or click blank then click option'
Abody
Burl
Cmethod
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Logging req.url instead of req.method.
Logging req.body which may be empty for GET requests.
3fill in blank
hard

Fix the error in the logging middleware to correctly log the request URL.

Express
app.use((req, res, next) => { console.log(req.[1]); next(); });
Drag options to blanks, or click blank then click option'
Arequest_url
Bpath
CrequestUrl
Durl
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.path which only gives the path without query string.
Using non-existent properties like requestUrl.
4fill in blank
hard

Fill both blanks to log the status code and response time after a request finishes.

Express
app.use((req, res, next) => {
  const start = Date.now();
  res.on('finish', () => {
    console.log(`Status: ${res.[1], Time: ${Date.now() - start}[2]`);
  });
  next();
});
Drag options to blanks, or click blank then click option'
AstatusCode
Bstatus
Cms
Dms()
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.status which is a function, not a property.
Using ms() which is not defined.
5fill in blank
hard

Fill all three blanks to create a simple logger that logs method, URL, and timestamp.

Express
app.use((req, res, next) => {
  const time = new Date().[1]();
  console.log(`[${time}] ${req.[2] ${req.[3]`);
  next();
});
Drag options to blanks, or click blank then click option'
AtoISOString
Bmethod
Curl
DgetTime
Attempts:
3 left
💡 Hint
Common Mistakes
Using getTime() which returns a number, not a string.
Mixing up method and url properties.