From d6422e7e733d487e1da478375f30bb8452cd5cc8 Mon Sep 17 00:00:00 2001 From: ogagaoghene emmanuel Date: Mon, 20 Oct 2025 11:04:44 +0100 Subject: [PATCH] Add: [Backend] ogaga/backend/nodeevents --- .../Backend/MiniProjects/NodeEvents/README.md | 224 ++++++++++++++++++ .../Backend/MiniProjects/NodeEvents/app.js | 12 + 2 files changed, 236 insertions(+) create mode 100644 Domains/Backend/MiniProjects/NodeEvents/README.md create mode 100644 Domains/Backend/MiniProjects/NodeEvents/app.js diff --git a/Domains/Backend/MiniProjects/NodeEvents/README.md b/Domains/Backend/MiniProjects/NodeEvents/README.md new file mode 100644 index 00000000..8a5610b9 --- /dev/null +++ b/Domains/Backend/MiniProjects/NodeEvents/README.md @@ -0,0 +1,224 @@ +**Contributor:** Ogagaoghene Esavwede +**Domain:** Backend +**Difficulty:** Beginner–Intermediate +**Tech Stack:** Node.js, Express.js, Pino + +# πŸš€ Express Logging Server with Pino + +--- + +## πŸ“ Description + +A simple Express.js server that replaces all `console.log` statements with a **structured logging system** using **Pino** β€” a fast, production-grade logging library for Node.js. +This project demonstrates how to create a centralized logger, use it throughout your app, and log useful events like route access and server startup. + +--- + +## 🎯 Features + +- βœ… Centralized logger configuration +- βœ… Structured JSON logs +- βœ… Replaces `console.log` with `logger.info`, `logger.error`, etc. +- βœ… Lightweight and high performance (using Pino) +- βœ… Works in production and development environments +- βœ… Easily extensible for file or external log management + +--- + +## πŸ› οΈ Tech Stack + +- **Node.js** – Runtime environment +- **Express.js** – Web framework +- **Pino** – Fast JSON logger + +--- + +## πŸš€ How to Run + +### Prerequisites + +- Node.js installed (v14 or higher) +- npm or yarn package manager + +### Installation Steps + +1. **Install dependencies** + + ```bash + npm install + ``` + +2. **Start the server** + + ```bash + npm start + ``` + +3. **Server runs at** + + ``` + http://localhost:3000 + ``` + +4. **See logs in terminal** + + ``` + {"level":30,"time":1729325400000,"msg":"Server running and listening on PORT: 3000","pid":12345,"hostname":"localhost"} + ``` + +--- + +## πŸ“ Project Structure + +``` +logging/ +β”œβ”€β”€ src/ +β”‚ β”œβ”€β”€ app.js # Main Express server file +β”‚ └── logging/ +β”‚ └── logger.js # Centralized logger configuration +β”œβ”€β”€ package.json # Dependencies and scripts +β”œβ”€β”€ .gitignore # Git ignore file +└── README.md # Documentation +``` + +--- + +## πŸ“‘ Example Usage + +### app.js + +```javascript +import express from "express"; +import logger from "./logging/logger.js"; + +const app = express(); +const PORT = process.env.PORT || 3000; + +app.get("/", (req, res) => { + logger.info("User hit home route"); + res.status(200).json({ success: true, message: "Welcome to the server" }); +}); + +app.listen(PORT, () => { + logger.info("Server running and listening on PORT: " + PORT); +}); +``` + +### logger.js + +```javascript +import pino from "pino"; + +const logger = pino({ + level: "info", +}); + +export default logger; +``` + +--- + +## πŸ’» Code Comparison + +### ❌ Old Way (Using `console.log`) + +```javascript +app.listen(PORT, () => { + console.log("Server running on port " + PORT); +}); +``` + +### βœ… New Way (Using `logger.info`) + +```javascript +app.listen(PORT, () => { + logger.info("Server running and listening on PORT: " + PORT); +}); +``` + +--- + +## πŸ“š Learning Outcomes + +### Skills Practiced + +- βœ… Structured logging in Node.js +- βœ… Using Pino for performance logging +- βœ… Creating reusable log modules +- βœ… Logging best practices + +### Concepts Learned + +- Importance of centralized logging +- Structured vs. unstructured logs +- Logging levels (`info`, `warn`, `error`, `debug`) +- Environment-based logging strategies + +--- + +## 🎨 Enhancement Ideas + +Want to improve this project? Try adding: + +1. **Pretty Logs** – Use `pino-pretty` for human-readable logs + + ```bash + npm install pino-pretty + npm start | npx pino-pretty + ``` + +2. **File Logging** – Pipe logs into a file: + + ```bash + node src/app.js > server.log + ``` + +3. **Environment-specific Levels** – Use `debug` level in dev, `info` in prod +4. **HTTP Request Logging** – Combine Pino with `pino-http` +5. **Error Tracking** – Send logs to monitoring tools like Logtail, Datadog, or Sentry + +--- + +## πŸ§ͺ Testing + +### Using curl + +```bash +curl http://localhost:3000/ +``` + +**Terminal Output:** + +```bash +{"level":30,"time":1729325400000,"msg":"User hit home route","pid":12345,"hostname":"localhost"} +``` + +--- + +## πŸš€ Future Enhancements + +- [ ] Add pretty-print logs in development +- [ ] Add log file rotation +- [ ] Add request logging middleware +- [ ] Add error logging middleware +- [ ] Add environment-based log filtering + +--- + +## πŸ“„ License + +MIT License β€” Free to use and modify! + +--- + +## 🀝 Contributing + +This project is open-source and created to help developers learn **how to replace `console.log` with a proper logger** in Node.js applications. +Feel free to: + +- Fork and enhance +- Report issues +- Suggest improvements +- Use it as a logging starter template + +--- diff --git a/Domains/Backend/MiniProjects/NodeEvents/app.js b/Domains/Backend/MiniProjects/NodeEvents/app.js new file mode 100644 index 00000000..33c44c64 --- /dev/null +++ b/Domains/Backend/MiniProjects/NodeEvents/app.js @@ -0,0 +1,12 @@ +const EventEmitter = require("events"); + +// Create an instance +const emitter = new EventEmitter(); + +// Listen for an event +emitter.on("greet", (name) => { + console.log(`Hello, ${name}!`); +}); + +// Emit (trigger) the event +emitter.emit("greet", "Ogaga");