Complete the code to log a message when the server starts.
app.listen(3000, () => { console.log([1]); });
app.listen inside console.log.The console.log function prints the message to the console when the server starts.
Complete the code to log the HTTP method of incoming requests.
app.use((req, res, next) => { console.log(req.[1]); next(); });req.url instead of req.method.req.body which may be empty for GET requests.The req.method property contains the HTTP method (GET, POST, etc.) of the request.
Fix the error in the logging middleware to correctly log the request URL.
app.use((req, res, next) => { console.log(req.[1]); next(); });req.path which only gives the path without query string.requestUrl.The correct property to get the full URL path is req.url. Other options are invalid and cause errors.
Fill both blanks to log the status code and response time after a request finishes.
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
console.log(`Status: ${res.[1], Time: ${Date.now() - start}[2]`);
});
next();
});res.status which is a function, not a property.ms() which is not defined.res.statusCode gives the HTTP status code. Adding 'ms' as a string shows milliseconds for response time.
Fill all three blanks to create a simple logger that logs method, URL, and timestamp.
app.use((req, res, next) => {
const time = new Date().[1]();
console.log(`[${time}] ${req.[2] ${req.[3]`);
next();
});getTime() which returns a number, not a string.method and url properties.toISOString() gives a readable timestamp. req.method and req.url log the request details.