Node.js is a powerful runtime environment that allows developers to run JavaScript on the server side. Whether you're a beginner or an experienced developer, this guide will help you get started with Node.js.
📦 Getting Started
Install Node.js
Download the latest version from Node.js official website and follow the installation instructions.Initialize a Project
Open your terminal and run:npm init -y
Create a Simple Script
Add ahello.js
file with the following code:console.log("Hello, Node.js!");
Run it using:
node hello.js
📚 Core Features
Event-Driven Architecture
Node.js uses an event loop to handle asynchronous operations efficiently.Non-blocking I/O
Node.js excels at handling multiple requests simultaneously without blocking.Module System
Use npm modules to extend functionality. For example:npm install express
🧱 Practical Examples
Build a Web Server
const http = require('http'); http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000);
Use Express Framework
Start with: ```bash npm init -y npm install express ```Then create a basic app:
const express = require('express'); const app = express(); app.get('/', (req, res) => res.send('Welcome to Node.js!')); app.listen(3000);