node-nestjs-structure
Node.js framework NestJS project structure
Configuration
- Create a
.env
file- Rename the .env.sample file to
.env
to fix it.
- Rename the .env.sample file to
- Edit env config
- Edit the file in the config folder.
default
,development
,production
,test
Installation
# 1. node_modules
npm ci
# 2. When import entities from an existing database
npm run entity
If you use multiple databases, modify them.
Development
npm run start:dev
Test
npm test # exclude e2e
npm run test:e2e
Production
# define NODE_ENV and PORT
npm run build
# NODE_ENV=production PORT=8000 node dist/app
node dist/app
# OR
npm start
Folders
+-- bin // Custom tasks
+-- dist // Source build
+-- public // Static Files
+-- src
| +-- config // Environment Configuration
| +-- entity // TypeORM Entities generated by `typeorm-model-generator` module
| +-- common // Global Nest Module
| | +-- controllers // Nest Controllers
| | +-- decorators // Nest Decorators
| | +-- dto // DTO (Data Transfer Object) Schema, Validation
| | +-- filters // Nest Filters
| | +-- guards // Nest Guards
| | +-- interceptors // Nest Interceptors
| | +-- interfaces // TypeScript Interfaces
| | +-- middleware // Nest Middleware
| | +-- pipes // Nest Pipes
| | +-- providers // Nest Providers
| | +-- * // models, repositories, services...
| +-- shared // Shared Nest Modules
| +-- * // Other Nest Modules, non-global, same as common structure above
+-- test // Jest testing
+-- typings // Modules or global type definitions
Implements
- See app, app.module
- Database
- Module Router
- Static Files
- Validation
- Global Authenticated Guard
- Global Exception Filter
- Global Logging Middleware
- Session Login with Passport
- Custom Logger for Production
- Role-based Guard
- AWS SDK Example
- Controller Routes
- Login
- Sample Parameter, DTO
- CRUD API Sample
- Database Query Example
- Unit Test
- E2E Test
Documentation
# APP, Compodoc
npm run doc #> http://localhost:8080
# API, Swagger - src/swagger.ts
npm run doc:api #> http://localhost:8000/api
File Naming for Class
export class PascalCaseSuffix {} //= pascal-case.suffix.ts
// Except for suffix, PascalCase to hyphen-case
class FooBarNaming {} //= foo-bar.naming.ts
class FooController {} //= foo.controller.ts
class BarQueryDto {} //= bar-query.dto.ts
Interface Naming
// https://stackoverflow.com/questions/541912
// https://stackoverflow.com/questions/2814805
interface User {}
interface CustomeUser extends User {}
interface ThirdCustomeUser extends CustomeUser {}