0
0
Expressframework~30 mins

Structured logging with JSON in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Structured logging with JSON in Express
📖 Scenario: You are building a simple Express server. To help with debugging and monitoring, you want to log requests in a structured JSON format. This makes logs easier to read and analyze by tools.
🎯 Goal: Create an Express server that logs each incoming request as a JSON object with keys for method, url, and timestamp.
📋 What You'll Learn
Create an Express app instance
Add a middleware to log requests in JSON format
Log method, url, and timestamp in ISO format
Start the server on port 3000
Print the JSON log to the console for each request
💡 Why This Matters
🌍 Real World
Structured JSON logs help developers and operations teams quickly understand server activity and diagnose issues by parsing logs with tools.
💼 Career
Logging is a key skill for backend developers and DevOps engineers to monitor and troubleshoot applications in production.
Progress0 / 4 steps
1
Setup Express app
Create a variable called express that requires the 'express' module. Then create a variable called app by calling express().
Express
Need a hint?

Use require('express') to import Express, then call it as a function to create the app.

2
Add JSON logging middleware
Add a middleware to app using app.use. The middleware should be a function with parameters req, res, and next. Inside it, create a variable logEntry that is an object with keys method (from req.method), url (from req.url), and timestamp (current time in ISO string format). Then log logEntry as a JSON string using console.log(JSON.stringify(logEntry)). Finally, call next().
Express
Need a hint?

The middleware function logs the request details as JSON and then calls next() to continue.

3
Add a simple route
Add a GET route on / to app using app.get. The route handler should send the text 'Hello World' as the response.
Express
Need a hint?

Use app.get with path '/' and send 'Hello World' as response.

4
Start the server
Start the Express server by calling app.listen on port 3000. Add a callback function that prints 'Server running on port 3000' to the console.
Express
Need a hint?

Use app.listen(3000, () => { console.log(...) }) to start the server.