Complete the code to import Morgan middleware in an Express app.
const express = require('express'); const morgan = require('[1]'); const app = express();
You import Morgan by requiring the 'morgan' package. This lets you use Morgan for logging HTTP requests.
Complete the code to use Morgan middleware with the 'dev' format in the Express app.
app.use([1]('dev'));
Use morgan('dev') to add Morgan middleware with the 'dev' logging format.
Fix the error in the code to log HTTP requests to a file using Morgan.
const fs = require('fs'); const path = require('path'); const accessLogStream = fs.createWriteStream(path.join(__dirname, '[1]'), { flags: 'a' }); app.use(morgan('combined', { stream: accessLogStream }));
The log file name should be a valid string like 'access.log' to store logs properly.
Fill both blanks to create a custom Morgan token and use it in the log format.
morgan.token('user-agent', function (req) { return req.headers['[1]']; }); app.use(morgan(':method :url :status :res[content-length] - :response-time ms :[2]'));
The header name is 'user-agent' and the token name used in the format must match it exactly.
Fill all three blanks to create a conditional Morgan logger that skips logging for requests with status code 304.
app.use(morgan('combined', { skip: function (req, res) { return res.statusCode [1] [2]; }, stream: [3] }));
The skip function returns true when status code is exactly 304, so Morgan skips logging those requests. The stream is set to the log file stream.