βš™οΈ Backend
Node.js

Node.js (opens in a new tab)

Node.js is a powerful JavaScript runtime built on Chrome's V8 engine that enables server-side execution of JavaScript code. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Architecture

Event Loop

  • Single-threaded event loop
  • Non-blocking I/O operations
  • Event-driven programming model
  • Asynchronous execution

Core Components

Key Features

Package Management

// Installing packages
npm install express
 
// Package.json example
{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.18.2"
  }
}

Asynchronous Programming

// Promises
async function fetchData() {
  try {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error:', error);
  }
}
 
// Event Emitters
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
 
myEmitter.on('event', () => {
  console.log('Event occurred!');
});

File System Operations

const fs = require('fs').promises;
 
async function readFile() {
  try {
    const data = await fs.readFile('file.txt', 'utf8');
    console.log(data);
  } catch (error) {
    console.error('Error reading file:', error);
  }
}

Performance Optimization

Memory Management

  • Garbage Collection: V8's memory management
  • Memory Leaks: Common pitfalls and detection
  • Heap Usage: Monitoring and optimization

Best Practices

  1. Use Clustering:
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
 
if (cluster.isMaster) {
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
} else {
  // Worker process code
}
  1. Implement Caching:
const NodeCache = require('node-cache');
const myCache = new NodeCache({ stdTTL: 100 });
 
// Set cache
myCache.set('key', obj);
 
// Get cached data
const value = myCache.get('key');

Debugging

Built-in Debugger

node --inspect app.js

Performance Profiling

const profiler = require('v8-profiler-next');
const fs = require('fs');
 
profiler.startProfiling('CPU Profile');
setTimeout(() => {
  const profile = profiler.stopProfiling();
  profile.export().pipe(fs.createWriteStream('./profile.cpuprofile'));
}, 30000);

Security Best Practices

  1. Keep Dependencies Updated
npm audit
npm update
  1. Use Security Headers
const helmet = require('helmet');
app.use(helmet());
  1. Input Validation
const { body, validationResult } = require('express-validator');
 
app.post('/user', [
  body('email').isEmail(),
  body('password').isLength({ min: 6 })
], (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }
});

Production Deployment

Environment Configuration

require('dotenv').config();
 
const config = {
  port: process.env.PORT || 3000,
  dbUrl: process.env.DATABASE_URL,
  nodeEnv: process.env.NODE_ENV
};

Process Management

// Using PM2
// ecosystem.config.js
module.exports = {
  apps: [{
    name: "app",
    script: "./app.js",
    instances: "max",
    exec_mode: "cluster",
    env: {
      NODE_ENV: "production"
    }
  }]
};

Additional Resources