../courses.php
Node
NODE108
90
Logs Save Careers
Use logs to understand what your Node server actually did.
Why do Node applications need logs?
Logs create a record of what happened. They help developers understand requests, errors, timing, failures, and unexpected behavior after the code has already run.
When a Node server works, users only see the response.
When something fails, the browser may show almost nothing useful.
Logs let the server tell its side of the story.
A good log answers: what happened, when it happened, and why it matters.
Without logs, production debugging becomes guessing.
Logging requests and errors
const http = require("node:http");
const server = http.createServer((request, response) => {
console.log(`${new Date().toISOString()} ${request.method} ${request.url}`);
try {
response.writeHead(200, {
"Content-Type": "application/json"
});
response.end(
JSON.stringify({
status: "ok"
})
);
} catch (error) {
console.error("Request failed:");
console.error(error.message);
response.statusCode = 500;
response.end("Server error");
}
});
server.listen(3000, () => {
console.log("Server listening on port 3000");
});
console.log() records normal activity.
new Date().toISOString() adds a timestamp.
request.method records whether the request was GET, POST, or another method.
request.url records which path was requested.
console.error() records problems separately from normal messages.
Evidence
Logs show what actually happened.
Debugging
Logs help find failures after they happen.
Production support
A live server needs records because you cannot watch every request manually.
Pattern spotting
Repeated errors, slow paths, and strange traffic become visible in logs.
No useful information
console.log("something happened");
Useful request log
console.log(`${new Date().toISOString()} ${request.method} ${request.url}`);
Ask ChatGPT: "Would these Node logs help diagnose a production problem?" Then check whether each log contains useful context.
Add a startup log when the server begins listening.
Log every request method.
Log every request URL.
Add an error log inside a catch block.
Make sure no secrets are printed in the logs.
Not logging enough to debug production issues.
Logging secrets, passwords, tokens, or private user data.
Writing vague logs that do not identify the request.
Using logs instead of proper error handling.
Ignoring logs until after a crisis.
Add useful Node logs for startup, requests, and errors while avoiding secret or private data leaks.
Login - Jit4All
Login
Welcome back to Jit4All