Skip to main content

Node.js

Node.js is a JavaScript runtime built on Chrome's V8 engine that enables running JavaScript outside the browser. It's the foundation of modern JavaScript tooling and a popular choice for building servers, APIs, and CLI tools.

Key Features

  • Event-driven: Non-blocking I/O for high concurrency
  • Single-threaded: Event loop handles async operations
  • npm ecosystem: Access to millions of packages
  • V8 engine: Fast JavaScript execution
  • Cross-platform: Runs on Windows, macOS, Linux

Common Use Cases

  • Web servers: Express, Fastify, Koa, Hapi
  • REST APIs: Backend for web and mobile apps
  • GraphQL: Apollo Server, Mercurius
  • Real-time apps: WebSocket servers with Socket.io
  • CLI tools: Build scripts, developer tools
  • Serverless: AWS Lambda, Vercel, Cloudflare Workers

Simple HTTP Server

const http = require('http');

const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Hello, World!' }));
});

server.listen(3000, () => {
console.log('Server running on port 3000');
});
FrameworkStyleBest For
ExpressMinimalGeneral purpose, most popular
FastifyPerformanceHigh-throughput APIs
NestJSOpinionatedEnterprise, Angular-like
KoaModernExpress successor, cleaner async
HapiConfigurationEnterprise, plugins

What We Like

  • JavaScript everywhere: Same language frontend and backend
  • Developer productivity: Fast development cycles
  • Performance: Excellent for I/O-bound workloads
  • Ecosystem: Package for nearly anything
  • Community: Active development and support

What We Don't Like

  • CPU-bound work: Single thread struggles with computation
  • Callback patterns: Legacy code can be messy (promises/async help)
  • Dependency management: Security and bloat concerns
  • Type safety: Requires TypeScript for robustness

Best Practices

  1. Use TypeScript: Catch errors early, improve maintainability
  2. Handle errors properly: Uncaught exceptions crash the process
  3. Use environment variables: Never commit secrets
  4. Validate input: Use Joi, Zod, or similar
  5. Monitor your app: APM, logging, health checks