Node.js to środowisko uruchomieniowe JavaScript zbudowane na silniku V8 Chrome. Pozwala uruchamiać JavaScript po stronie serwera. Node.js jest event-driven i non-blocking — obsługuje tysiące jednoczesnych połączeń na jednym wątku.
Node.js jest fundamentem ekosystemu: Next.js, Nest.js, Express, Fastify, Prisma, tRPC. Jeden język (TypeScript) na froncie i backendzie = shared types, szybszy development, mniejszy zespół.
import express from 'express';
import { z } from 'zod';
const app = express();
app.use(express.json());
const CreateUserSchema = z.object({
name: z.string().min(2),
email: z.string().email(),
age: z.number().min(18),
});
app.post('/api/users', async (req, res) => {
const result = CreateUserSchema.safeParse(req.body);
if (!result.success) {
return res.status(400).json(result.error.flatten());
}
const user = await db.user.create({ data: result.data });
res.status(201).json(user);
});
app.listen(3000);
import { Controller, Get, Post, Body, Param } from '@nestjs/common';
@Controller('users')
export class UsersController {
constructor(private usersService: UsersService) {}
@Get()
async findAll() {
return this.usersService.findAll();
}
@Get(':id')
async findOne(@Param('id') id: string) {
return this.usersService.findOne(id);
}
@Post()
async create(@Body() dto: CreateUserDto) {
return this.usersService.create(dto);
}
}
// schema.prisma
model User {
id String @id @default(uuid())
name String
email String @unique
orders Order[]
createdAt DateTime @default(now())
}
// TypeScript query — full type safety
const users = await prisma.user.findMany({
where: { orders: { some: { status: 'completed' } } },
include: {
orders: {
where: { status: 'completed' },
orderBy: { createdAt: 'desc' },
take: 5,
},
},
});
import { Server } from 'socket.io';
const io = new Server(server, { cors: { origin: '*' } });
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
socket.on('join-room', (roomId: string) => {
socket.join(roomId);
socket.to(roomId).emit('user-joined', socket.id);
});
socket.on('message', ({ roomId, text }) => {
io.to(roomId).emit('message', {
userId: socket.id,
text,
timestamp: Date.now(),
});
});
});
Express, Fastify, Nest.js, tRPC. Middleware, validation, auth, rate limiting.
Server components, API routes, SSR/SSG, middleware. Jeden deploy = front + back.
Socket.io, WebSockets, Server-Sent Events. Chat, notifications, live dashboards.
Lekkie serwisy, message queues (BullMQ, RabbitMQ), event-driven architecture.
Prisma, Drizzle, TypeORM, Knex. PostgreSQL, MongoDB, Redis. Migracje, seedy.
Vercel Functions, AWS Lambda, Cloud Functions. Cold start ~100ms. Pay-per-invocation.
Idealny gdy: budujesz web app (full-stack z Next.js), potrzebujesz real-time features, chcesz jeden język na froncie i backendzie, budujesz API dla SPA/mobile.
Nie najlepszy gdy: potrzebujesz heavy CPU computation (lepiej Go/Rust), budujesz ML pipeline (lepiej Python), potrzebujesz ekstremalnej wydajności (lepiej Go).