Node.js
Why Node.js?
Section titled “Why Node.js?”- ✅ Same language frontend & backend
- ✅ Event-driven, non-blocking I/O
- ✅ Huge npm ecosystem
- ✅ Great for real-time apps
- ✅ Easy to learn for JS developers
Simple HTTP Server
Section titled “Simple HTTP Server”const http = require('http');
const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello from Node.js!');});
server.listen(8000, () => { console.log('Server running on :8000');});Express.js
Section titled “Express.js”const express = require('express');const app = express();
app.get('/', (req, res) => { res.send('Hello from Express!');});
app.listen(8000, () => { console.log('Server running on :8000');});PM2 Deployment
Section titled “PM2 Deployment”# Install PM2npm install -g pm2
# Start apppm2 start server.js --name snapcode
# Cluster mode (use all CPUs)pm2 start server.js -i max --name snapcode
# Save & startuppm2 savepm2 startupecosystem.config.js
Section titled “ecosystem.config.js”module.exports = { apps: [{ name: 'snapcode', script: 'server.js', instances: 'max', exec_mode: 'cluster', env: { NODE_ENV: 'production', PORT: 8000 } }]};Event Loop
Section titled “Event Loop”// Non-blocking I/Oconst fs = require('fs');
// Async (non-blocking)fs.readFile('file.txt', (err, data) => { console.log(data);});
// This runs immediately, doesn't waitconsole.log('Reading file...');vs Other Runtimes
Section titled “vs Other Runtimes”| Aspect | Node.js | Python | Go | PHP |
|---|---|---|---|---|
| Model | Event loop | Sync/Async | Goroutines | Process pool |
| Best For | Real-time, APIs | Scripts, ML | Microservices | Web pages |
| Scaling | PM2 cluster | Gunicorn | Built-in | FPM |
| Memory | Medium | Medium | Low | Per-request |