NestJS Local Development Guide

Build a TypeScript backend API with NestJS on localhost:3000, supporting REST, WebSocket, and microservices architecture.


NestJS is an enterprise-level backend framework based on Node.js, using TypeScript, with an architecture similar to Angular (modules, dependency injection). The default local development server runs at http://localhost:3000.

Default localhost Access

PurposeAddress
Development Serverhttp://localhost:3000
Swagger (if enabled)http://localhost:3000/api

The port can be modified in main.ts: await app.listen(3000).

Quick Start

npm i -g @nestjs/cli
nest new my-api
cd my-api
npm run start:dev

The start:dev command enables watch mode, automatically reloading the code on changes.

Project Structure (Brief)

src/
├── app.module.ts
├── app.controller.ts
├── app.service.ts
└── main.ts          # Entry point, listen port

Database Integration

Common combinations:

DatabasePackageConnection
PostgreSQL@nestjs/typeorm + pglocalhost:5432
MySQLTypeORM + mysql2localhost:3306
MongoDB@nestjs/mongooselocalhost:27017
Redis@nestjs/bull + ioredislocalhost:6379

Example .env file:

DATABASE_HOST=localhost
DATABASE_PORT=5432
REDIS_HOST=localhost
REDIS_PORT=6379

Relationship with Express

NestJS uses Express by default (can switch to Fastify). Compared to writing Express by hand, NestJS offers modularity, decorator routing, and built-in testing support, making it suitable for medium to large API projects.

Frontend and Backend Separation

When using Vite (5173) with NestJS API (3000), configure Vite’s proxy or enable CORS in NestJS:

app.enableCors({ origin: 'http://localhost:5173' });

Frequently Asked Questions

Port 3000 is occupied by Next.js
Change Nest to use 3001, or stop other Node processes.

TypeORM connection failed
Ensure PostgreSQL/MySQL is running; in Docker Compose, the host should still use localhost (from the host’s perspective).

Summary

Running NestJS locally with npm run start:dev, the default is http://localhost:3000, making it a common choice for TypeScript backend APIs and microservices.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run