Node.js is a powerful JavaScript runtime environment that allows developers to run JavaScript outside the browser, mainly on servers. It is widely used to build fast, scalable, and real-time applications such as chat apps, APIs, streaming services, and more.
1. What is Node.js?
Node.js is built on Google Chrome’s V8 JavaScript engine and enables JavaScript to be used for server-side development. Unlike traditional server technologies, Node.js uses a non-blocking, event-driven architecture, making it very efficient.
2. Features of Node.js
- Fast execution using V8 engine
- Asynchronous and non-blocking I/O
- Single-threaded but highly scalable
- Large ecosystem of packages (npm)
- Cross-platform support
- Ideal for real-time applications
3. Installing Node.js
- Download Node.js from the official website
- Install it on your system
- Verify installation:
node -v
npm -v
4. Node.js Architecture
Node.js works on an event-driven model:
- Client requests are placed in an event queue
- Event loop processes requests asynchronously
- No thread blocking, resulting in high performance
5. Your First Node.js Program
console.log("Hello, Node.js!");
Explanation:console.log() prints output to the terminal.
6. Node.js Modules
Modules allow code reusability.
Built-in Modules
- fs (File System)
- http
- path
- os
Example: Using HTTP Module
const http = require("http");
http.createServer((req, res) => {
res.write("Welcome to Node.js");
res.end();
}).listen(3000);
7. Creating Custom Modules
// math.js
exports.add = (a, b) => a + b;
const math = require("./math");
console.log(math.add(5, 3));
8. npm (Node Package Manager)
npm is used to install and manage packages.
npm init
npm install express
9. File System (fs) Module
Used to handle files.
Reading a File
const fs = require("fs");
fs.readFile("data.txt", "utf8", (err, data) => {
console.log(data);
});
10. Events in Node.js
Node.js handles operations using events.
const events = require("events");
const eventEmitter = new events.EventEmitter();
eventEmitter.on("greet", () => {
console.log("Hello Event!");
});
eventEmitter.emit("greet");
11. Asynchronous Programming
Node.js supports async programming using:
- Callbacks
- Promises
- async/await
Example
async function fetchData() {
return "Data received";
}
12. Streams in Node.js
Streams handle large data efficiently.
Types of Streams:
- Readable
- Writable
- Duplex
- Transform
const fs = require("fs");
const stream = fs.createReadStream("file.txt");
stream.pipe(process.stdout);
13. Express.js Framework
Express is a lightweight Node.js framework for building APIs.
Installing Express
npm install express
Basic Express Server
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello Express");
});
app.listen(3000);
14. Routing in Express
app.get("/about", (req, res) => {
res.send("About Page");
});
15. Middleware in Express
Middleware functions execute between request and response.
app.use((req, res, next) => {
console.log("Middleware executed");
next();
});
16. Handling JSON & Forms
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
17. REST API in Node.js
Node.js is widely used for creating RESTful APIs.
app.get("/api/users", (req, res) => {
res.json({ name: "John", age: 25 });
});
18. Working with Databases
Node.js supports many databases:
- MongoDB
- MySQL
- PostgreSQL
MongoDB Example
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/test");
19. Authentication & Security
- Use JWT for authentication
- Hash passwords using bcrypt
- Use HTTPS
- Validate user input
20. Error Handling
app.use((err, req, res, next) => {
res.status(500).send("Something went wrong");
});
21. Environment Variables
npm install dotenv
require("dotenv").config();
console.log(process.env.PORT);
22. Advantages of Node.js
- High performance
- Scalable applications
- Same language for frontend & backend
- Strong community support
23. Disadvantages of Node.js
- Not ideal for CPU-intensive tasks
- Callback complexity (callback hell)
- Requires good async handling knowledge
24. Real-World Applications
- Chat applications
- REST APIs
- Real-time dashboards
- Streaming platforms
- IoT applications
25. Conclusion
Node.js is a powerful and modern backend technology that enables developers to build fast, scalable, and efficient server-side applications using JavaScript. With frameworks like Express and a huge npm ecosystem, Node.js is an excellent choice for both beginners and professionals.