0
0
Expressframework~10 mins

Morgan for HTTP request logging in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Morgan for HTTP request logging
Incoming HTTP Request
Morgan Middleware Intercepts
Start Timer & Pass to Next
Route Handler Responds
Format & Output Log on 'finish'
Morgan middleware catches each HTTP request, starts a timer, passes control to the next middleware/route handler. When the response 'finish' event fires, it formats a log line and outputs it.
Execution Sample
Express
const express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('tiny'));
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
This code sets up an Express server that logs each HTTP request in a short format using Morgan.
Execution Table
StepActionRequest MethodRequest URLMorgan Log OutputNext Step
1HTTP GET request arrivesGET/Morgan middleware intercepts
2Morgan starts timerGET/Pass control to route handler
3Route handler sends responseGET/Response 'finish' event
4Morgan formats log entryGET/GET / 200 - - 3.45 msOutput log to console
5Request completeGET/End of request cycle
💡 Request completes after response sent; Morgan logs each request on response 'finish' event.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
req.methodundefinedGETGETGETGET
req.urlundefined////
morganLogundefinedundefinedundefinedundefinedGET / 200 - - 3.45 ms
Key Moments - 2 Insights
Why does Morgan log after the route handler sends the response?
Morgan logs on the response 'finish' event (Step 4 in execution_table), after the route handler (Step 3). This captures status code and response time accurately.
What does the 'tiny' format in Morgan show in the log?
The 'tiny' format outputs method, URL, status code, response time, and a few dashes for missing info. See Morgan Log Output column in Step 4 for example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Morgan log output at Step 4?
AGET / 200 - - 3.45 ms
BPOST /api 404 - - 10 ms
CError: Request failed
DNo log output yet
💡 Hint
Check the 'Morgan Log Output' column at Step 4 in the execution_table.
At which step does Morgan pass control to the next middleware or route handler?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Next Step' column to see when control passes to route handler.
If the request method was POST instead of GET, how would the Morgan log output change at Step 4?
AIt would still show GET / 200 - - 3.45 ms
BMorgan would not log POST requests
CIt would show POST / 200 - - 3.45 ms
DIt would show GET / 404 - - 3.45 ms
💡 Hint
Morgan logs the actual request method from req.method as shown in variable_tracker.
Concept Snapshot
Morgan logs HTTP requests in Express apps.
Use app.use(morgan('format')) to add logging.
Logs show method, URL, status, and response time.
Intercepts before but logs after route handlers on response 'finish'.
Helps track and debug incoming requests easily.
Full Transcript
Morgan is a middleware for Express that logs HTTP requests. When a request arrives, Morgan intercepts it, starts a timer, passes control to the next middleware or route handler. When the response finishes ('finish' event), it formats a log line with method, URL, status, and response time, then outputs this log to the console or a file. This helps developers see request details in real time and debug their apps. The 'tiny' format is a short log style showing key info. Morgan logs on response finish, capturing accurate timing and status even if errors occur. Changing the request method changes the log output accordingly.