0
0
Expressframework~10 mins

Morgan for HTTP request logging 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 Morgan middleware in an Express app.

Express
const express = require('express');
const morgan = require('[1]');
const app = express();
Drag options to blanks, or click blank then click option'
Aexpress
Blogger
Chttp
Dmorgan
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'morgan' in require.
Using a wrong package name like 'logger'.
2fill in blank
medium

Complete the code to use Morgan middleware with the 'dev' format in the Express app.

Express
app.use([1]('dev'));
Drag options to blanks, or click blank then click option'
Aexpress
Blogger
Cmorgan
Dhttp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'morgan'.
Forgetting to call the function with parentheses.
3fill in blank
hard

Fix the error in the code to log HTTP requests to a file using Morgan.

Express
const fs = require('fs');
const path = require('path');
const accessLogStream = fs.createWriteStream(path.join(__dirname, '[1]'), { flags: 'a' });
app.use(morgan('combined', { stream: accessLogStream }));
Drag options to blanks, or click blank then click option'
Aaccess.log
Baccesslog
Clog_access.txt
Daccess_log
Attempts:
3 left
💡 Hint
Common Mistakes
Using file names without extensions.
Using invalid or missing file names.
4fill in blank
hard

Fill both blanks to create a custom Morgan token and use it in the log format.

Express
morgan.token('user-agent', function (req) { return req.headers['[1]']; });
app.use(morgan(':method :url :status :res[content-length] - :response-time ms :[2]'));
Drag options to blanks, or click blank then click option'
Auser-agent
Bcontent-type
Dreferer
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'content-type' or 'referer' instead of 'user-agent'.
Mismatching token names in definition and format.
5fill in blank
hard

Fill all three blanks to create a conditional Morgan logger that skips logging for requests with status code 304.

Express
app.use(morgan('combined', {
  skip: function (req, res) {
    return res.statusCode [1] [2];
  },
  stream: [3]
}));
Drag options to blanks, or click blank then click option'
A===
B304
CaccessLogStream
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using !== instead of === in skip condition.
Using wrong status code number.
Not setting the stream correctly.