Suggest an editImprove this articleRefine the answer for “Working with a server”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Node.js** lets you build HTTP/HTTPS servers, TCP/UDP connections, WebSocket servers and REST/GraphQL APIs without external dependencies, plus work with files, processes and databases. **Key point:** the built-in `http`, `net`, `https`, `fs` and `child_process` modules cover most server tasks without frameworks.Shown above the full answer for quick recall.Answer (EN)Image## 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: | Type | Example libraries | |---|---| | SQL | `pg` (PostgreSQL), `mysql2`, `sequelize`, `prisma` | | NoSQL | `mongoose` (MongoDB), `redis`, `couchdb` | | In-memory | `redis`, `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 | Capability | Node.js can do it | |---|---| | Building HTTP/HTTPS servers | Yes | | Handling TCP/UDP connections | Yes | | Working with files | Yes | | Asynchronous operations | Yes | | Working with databases | Yes | | WebSockets and real time | Yes | | Microservices, REST, GraphQL | Yes | | Extending via npm packages | Yes |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.