What is NestJS?
A progressive Node.js framework for building efficient, reliable and scalable server-side applications.
1. Controllers:
Are responsible for handling inconming requests and returning responded to the client.
3. Guards:
Is a class annotated with the @Injectable() decorator.
Guards should implement the CanActivate interface.
2. Modules:
Is a class annotated with a @Module decorator.
Provides metadata that Nest makes user of to organize the application structure.
4. Middlewares:
Is a function which is called before the route handler.
5. Pipes:
Is a class annotated with the @Injectable() decorator.
6. Database:
Allowing you to easily integrate with any SQL or NoSQL database.
CLI
npm i -g @nest/cli
Use
nest new project-dominicode
platform agnostic
Websockets
The Building Blocks
It has all ..
TypeScript
RxJS
Angular
NestJS
Controller
export class CatController {
constructor
(private readonly catService: CatsService){}
@ApiTags('cat')
@Post('')
async createCat(@Body() cat: CreateCatDto) {
return this.catService.create(cat);
}
Service
@Injectable()
export class CatsService {
private readonly cats: Cat[] = [];
create(cat: Cat) {
this.cats.push(cat);
}
}
Module
@Module({
controllers: [CatController],
providers: [CatsService],
})
export class CatsModule {
}
Bootstrap
async function bootstrap() {
const opts: NestApplicationOptions = {};
const app = await NestFactory.create
<NestExpressApplication>(AppModule, opts);
app.disable('x-powered-by');
await app.listen(LISTEN_PORT);
}
bootstrap();
Adaptors
async function bootstrap() {
const app = await
NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
await app.listen(3000);
}
Microservice
async function bootstrap() {
const app = await
NestFactory.createMicroservice
<MicroserviceOptions>(
AppModule,
{
transport: Transport.TCP,
},
);
await app.listen();
}