Node.js Fundamentals
Week 11 covered Node.js core modules and finished the weather CLI from last week.
Core Modules
File System Module
The fs module handles all file operations. Reading files, writing data, checking existence, creating directories, and getting file stats.
Using fs/promises makes everything work with async/await. Clean async code without callbacks.
Error handling is critical. File operations fail often. Files don't exist, permissions are incorrect, and the disk is full. Every operation needs try/catch.
Path Module
The path module handles file paths correctly across operating systems. Windows uses backslashes, Unix uses forward slashes. Path handles this automatically.
Join builds paths correctly. Resolve gets absolute paths. Dirname, basename, and extname extract path components.
Events Module
EventEmitter is how Node.js handles events internally. You create events, listen for them, and emit them when things happen.
This pattern appears everywhere in Node.js. Streams use events, servers use events, and core modules are built on EventEmitter.
Understanding events changed how I see Node.js architecture. It's event-driven at the core.
CommonJS vs ES6 Modules
The difference between module systems was interesting, especially **with __filename and __dirname.
In CommonJS, __filename and __dirname are available as globals. They give you the current file path and directory.
In ES6 modules, these don't exist. You need to use import.meta.url and path functions to get the same information. Different approach for the same result.
This matters when migrating old Node.js code to ES6 modules. Code that uses __dirname directly will break.
Weather CLI Project
Finished the weather CLI from Week 10.
The tool fetches weather data from OpenWeather API and displays it in the terminal. Pass a city name, get the current weather.
Features: fetch by city, handle invalid cities with errors, display temperature, show conditions and humidity, and format output readably.
Used fetch for API requests, process.argv for command line arguments, and fs for configuration.
HTTP Basics
Started learning HTTP fundamentals and building servers with the http module.
Understanding request and response. Clients send requests with method, URL, and headers. Servers send responses with a status code, headers, and body.
Status codes matter. 200 for success, 404 for not found, 500 for errors, 201 for created.
Handling different HTTP methods. GET for retrieving, POST for creating, PUT for updating, and DELETE for removing.
Built a basic server handling different routes and methods. Routing based on URL and method. Parsing request bodies. Setting correct status codes and content types.
Understanding HTTP before frameworks makes everything clearer.
Key Lessons
File operations need error handling. Things fail in production.
The Path module prevents cross-platform bugs. Never build paths manually.
Events are fundamental to Node.js. EventEmitter is everywhere.
ES6 modules handle __filename and _ _ dirname differently from CommonJS.
HTTP is simpler than it seems. Request in, response out.
Moving Forward
Starting Express.js and building APIs. Moving from the raw HTTP module to proper frameworks.
Question: What Node.js concept was hardest for you to understand?Share**
Top comments (0)