0
0
Expressframework~10 mins

Performance monitoring basics 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 import the Express module.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Ahttp
Bpath
Cfs
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' will import the Node.js HTTP module, not Express.
Using 'fs' or 'path' imports file system or path modules, unrelated to Express.
2fill in blank
medium

Complete the code to create a new Express application instance.

Express
const app = [1]();
Drag options to blanks, or click blank then click option'
Ahttp
Bexpress
Crouter
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' or 'server' will cause errors because they are not functions here.
Using 'router' is for routing, not creating the app instance.
3fill in blank
hard

Fix the error in the code to log the response time using middleware.

Express
app.use((req, res, next) => {
  const start = Date.now();
  res.on('finish', () => {
    console.log(`Response time: ${Date.now() - [1]ms`);
  });
  next();
});
Drag options to blanks, or click blank then click option'
AstartTime
Bres.start
Cstart
Dreq.start
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables like 'startTime' causes errors.
Using properties on req or res that don't exist will not work.
4fill in blank
hard

Fill both blanks to create a middleware that measures and logs request duration in milliseconds.

Express
app.use((req, res, next) => {
  const [1] = Date.now();
  res.on('[2]', () => {
    console.log(`Request took ${Date.now() - start}ms`);
  });
  next();
});
Drag options to blanks, or click blank then click option'
Astart
Bbegin
Cfinish
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'begin' instead of 'start' for the variable name is uncommon and may confuse.
Listening to 'close' event instead of 'finish' may not always indicate response completion.
5fill in blank
hard

Fill all three blanks to create a simple route that responds with 'OK' and logs the request method and URL.

Express
app.[1]('/status', (req, res) => {
  console.log(`[2] request to [3]`);
  res.send('OK');
});
Drag options to blanks, or click blank then click option'
Aget
BPOST
Creq.url
Dreq.method
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'POST' in the method call causes errors; it should be lowercase 'get'.
Swapping req.method and req.url in the log message changes meaning.