Skip to main content

Working with a server

1. Building HTTP servers

Node.js lets you build your own web servers with no external dependencies.

javascript
const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello from Node.js server!'); }); server.listen(3000, () => { console.log('Server running at http://localhost:3000'); });

What this means:

  • You can handle requests and responses directly.
  • Full control over headers, statuses, routes, and so on.
  • Easy to build a REST API, GraphQL API or WebSocket server.

2. Networking (TCP, UDP, HTTPS)

The net module lets you build low-level TCP/UDP servers:

javascript
const net = require('net'); const server = net.createServer(socket => { socket.write('Connected to TCP server'); socket.on('data', data => console.log('Received:', data.toString())); }); server.listen(8080);
  • Support for TCP/UDP protocols
  • Can build proxies, chat servers, data streaming

3. HTTPS and SSL/TLS

Node.js lets you build secure HTTPS servers:

javascript
const https = require('https'); const fs = require('fs'); const options = { key: fs.readFileSync('private.key'), cert: fs.readFileSync('certificate.crt') }; https.createServer(options, (req, res) => { res.end('Secure connection established'); }).listen(443);
  • Support for SSL/TLS certificates
  • You can build secure REST APIs and web services

4. Filesystem access (fs)

Node.js has built-in functions for reading, writing and streaming files:

javascript
const fs = require('fs'); // Reading a file fs.readFile('data.txt', 'utf8', (err, data) => console.log(data)); // Writing a file fs.writeFileSync('output.txt', 'Server log data');

This matters for:

  • logging,
  • storing data,
  • working with templates and static files.

5. Working with processes and the system (os, process, child_process)

Node.js can interact with the operating system:

  • Get information about the server:

    javascript
    const os = require('os'); console.log(os.cpus(), os.freemem());
  • Run external commands:

    javascript
    const { exec } = require('child_process'); exec('ls', (err, stdout) => console.log(stdout));
  • Manage environment variables and I/O streams

6. Routing and APIs (via frameworks)

Node.js can be extended with frameworks:

  • Express.js - building REST APIs
  • NestJS - a modular architecture for complex servers
  • Fastify - high-performance servers
  • Hapi, Koa - alternatives for specific needs

An Express.js example:

javascript
const express = require('express'); const app = express(); app.get('/users', (req, res) => res.json([{ id: 1, name: 'John' }])); app.listen(3000);

7. Working with databases

Node.js supports working with virtually every kind of database:

TypeExample libraries
SQLpg (PostgreSQL), mysql2, sequelize, prisma
NoSQLmongoose (MongoDB), redis, couchdb
In-memoryredis, sqlite

8. Real time and WebSocket

Node.js is a great fit for real-time applications:

javascript
const { Server } = require('socket.io'); const io = new Server(3000); io.on('connection', socket => { console.log('Client connected'); socket.emit('message', 'Welcome!'); });

You can build:

  • online chats
  • game servers
  • real-time tracking
  • notifications and live updates

9. Asynchrony and scalability

  • Node.js uses the event loop and asynchronous operations, so it does not block the thread.
  • It can handle thousands of connections at once.
  • It supports clustering (the cluster module) to spread load across CPU cores.

Summary

CapabilityNode.js can do it
Building HTTP/HTTPS serversYes
Handling TCP/UDP connectionsYes
Working with filesYes
Asynchronous operationsYes
Working with databasesYes
WebSockets and real timeYes
Microservices, REST, GraphQLYes
Extending via npm packagesYes

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.