This repository contains beginner-friendly examples of important Node.js concepts such as:
- OS Module
- Path Module
- File System (FS)
- HTTP Server
- Routing
- Asynchronous Programming
- Callback Functions
- TCP Server
Perfect for learning backend development with Node.js.
π nodejs-learning
β£ π index.js
β£ π README.mdgit clone https://github.com/your-username/nodejs-learning.gitDownload and install Node.js from:
node index.jsThe OS module provides information about the operating system.
const os = require('os');
console.log(os.version());
console.log(os.freemem());
console.log(os.homedir());
console.log(os.hostname());- Get OS version
- Get free memory
- Get home directory
- Get system hostname
The Path module helps handle file and directory paths.
const path = require('path');
const filepath = 'C:\\Users\\file.txt';
console.log(path.basename(filepath));
console.log(path.dirname(filepath));
console.log(path.extname(filepath));| Method | Description |
|---|---|
| basename() | Returns file name |
| dirname() | Returns folder path |
| extname() | Returns file extension |
Used for creating, reading, updating, and deleting files.
const fs = require('fs');
fs.writeFile('Node.txt', 'This is Node js File', (err) => {
if(err) throw err;
console.log("File Created");
});fs.readFile('Node.txt', 'utf-8', (err, data) => {
console.log(data);
});fs.unlink('Node.txt', (err) => {
console.log("File deleted");
});Node.js can create web servers using the HTTP module.
const http = require('http');
const server = http.createServer((req, res) => {
res.end("Server Running");
});
server.listen(4000, () => {
console.log("Server started");
});http://localhost:4000Routing allows different responses for different URLs.
if(url == '/'){
res.end("Home Page");
}
else if(url == '/contact'){
res.end("Contact Page");
}
else{
res.end("404 Not Found");
}| Route | Description |
|---|---|
| / | Home Page |
| /contact | Contact API |
| /product | Product API |
Node.js executes asynchronous operations without blocking the main thread.
console.log("Program 1");
setTimeout(() => {
console.log("Program 2");
}, 2000);
console.log("Program 3");- Event Loop
- Non-blocking code
- Timers
- Async execution
A callback is a function passed into another function.
function Employee(name, callback){
console.log(name);
callback();
}
Employee("Sam", function(){
console.log("Welcome");
});TCP server communication using Node.js Net module.
import net from "node:net";
const server = net.createServer();
server.listen(4000, () => {
console.log("TCP Server Started");
});- Node.js
- JavaScript
- HTTP Module
- File System Module
- TCP Networking
After completing this project, you will understand:
β
Core Node.js modules
β
Server creation
β
File handling
β
Routing
β
Asynchronous programming
β
Callback functions
β
TCP communication
- Add Express.js
- Add MongoDB
- Build REST APIs
- Authentication System
- Real-time Chat Application
Contributions are welcome.
- Fork repository
- Create new branch
- Commit changes
- Push branch
- Create Pull Request
This project is licensed under the MIT License.
Fulbabu Islam
- π Portfolio: https://fulbabu.dev/
- π» GitHub: https://github.com/fulbabu-t
- π LinkedIn: https://www.linkedin.com/in/fulbabu-islam-96a9ba2ba