Simple NestJS backend for study.
- Email: admin@example.com
- Password: 123456
Prerequisites
- Docker & Docker Compose installed
- (For running tests locally) Node.js 18+ and npm
Environment
- Configure
.envin the project root. Example values are already provided in.envand aligned withdocker-compose.yml(DB on port 3306, DB_NAME=nest, DB_USER=root, DB_PASS=password).
Run (development)
-
Option A — install dependencies on host (recommended for fast dev; container will use host node_modules):
- Open cmd.exe in project folder:
npm ci docker-compose up - If container already running, restart the app service:
docker-compose restart app
- Open cmd.exe in project folder:
-
Option B — let the image install dependencies (image contains node_modules in a volume):
- Build and run:
docker-compose down -v docker-compose up --build - To force a fresh install without cache:
docker-compose build --no-cache docker-compose up
- Build and run:
Notes on node_modules and caching
- If you prefer to install deps on host and avoid rebuilding images when adding packages, use Option A.
- If you use Option B, avoid mounting host code in a way that hides the container
node_modules. If you encounter missing packages inside the container (e.g.class-transformer), either install on host (npm ci) or rebuild the image.
Database persistence
- The compose file uses a named volume for MySQL (db_data). To keep DB data across restarts, do NOT run:
(that removes volumes and deletes DB data).
docker-compose down -v - To back up the DB:
docker exec -i mysql_db mysqldump -u root -ppassword nest > backup_nest.sql - To restore:
docker exec -i mysql_db mysql -u root -ppassword nest < backup_nest.sql
Run tests (local)
- Unit + integration tests:
npm test - E2E tests:
npm run test:e2e - If running tests inside the running container:
docker exec -it nestjs_app npm test docker exec -it nestjs_app npm run test:e2e
Troubleshooting
- If Swagger UI at
/api/docsshows empty schemas, ensure DTOs/controllers have@ApiProperty()and@ApiTags()(files are updated). - If container reports missing package right after
docker-compose up --build, run:or install on host (docker-compose down -v docker-compose up --buildnpm ci) anddocker-compose up.
Useful commands
- Start services in background:
docker-compose up -d - View logs:
docker-compose logs -f app - Stop services:
docker-compose down
The server enables CORS globally in main.ts.
Environment variable: CORS_ORIGINS
Format: comma-separated list of allowed origins.
Example:
CORS_ORIGINS=http://localhost:5173,http://localhost:4200
Behavior:
- If
CORS_ORIGINSis set, only those origins are allowed. - If not set, all origins are allowed (development convenience). For production, set an explicit list.
Headers allowed: Content-Type, Authorization
Credentials: enabled (credentials: true). If your frontend needs cookies, keep this. If you only use Authorization headers, it's still fine.
If you face a CORS error in the browser:
- Confirm the frontend origin matches exactly (scheme, host, port) one of the entries in
CORS_ORIGINS. - Ensure no trailing slashes in the origin list.
- Restart the backend after changing env vars.