NestJS

A progressive NodeJS framework


Jan Oelschlegel - NodeJS Meetup Leipzig - 08/2019

Wer ist dieser Typ ?


  • Master-Student Medieninformatik @ HTWK Leipzig
  • Werksstudent @ nextbike Gmbh
  • glücklicher NestJS-Nutzer


Xing: https://www.xing.com/profile/Jan_Oelschlegel

NestJS ?

Nest (or NestJS) is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).

Key-Words

  • progressive
  • scalable
  • versatile
  • extensible
  • highly testable
  • CLI
  • dependency injection
  • documentation

Wer hat's erfunden ?

Kamil

Erst die Theorie...

Alles hat einen Anfang...


					import { NestFactory } from '@nestjs/core';
					import { AppModule } from './app.module';
					
					async function bootstrap() {
					  const app = await NestFactory.create(AppModule);
					  await app.listen(3000);
					}
					bootstrap();

				

Komponenten

  • Module
  • Controller
  • Provider
  • Guards
  • Pipes
  • Interceptors
  • Middleware
  • Exception Filter

Module

Modules


							import { Module } from '@nestjs/common';

							@Module({
								imports: [DatabaseModule],
								controllers: [MeetupController],
								providers: [MeetupService],
								exports: [MeetupService],
							})
							export class MeetupModule {}

													

Controller

Controller



							import { Controller, Get } from '@nestjs/common';

							@Controller('meetup')
							export class MeetupController {
							  @Get()
							  findAll(): string {
								return 'Hello Meetup';
							  }
							}
	
					

Provider

Provider


	

							import { Injectable } from '@nestjs/common';
							
							@Injectable()
							export class MeetupService {
							  private readonly meetups: string[] = [];
							
							  create(meetup: string) {
								this.meetups.push(meetup);
							  }
							
							  findAll(): string[] {
								return this.meetups;
							  }
							}
		
						

Guards

Guards

							

									import { Injectable, 
										CanActivate, 
										ExecutionContext } from '@nestjs/common';
									import { Observable } from 'rxjs';
									
									@Injectable()
									export class AuthGuard implements CanActivate {
									  canActivate(
										context: ExecutionContext,
									  ): boolean | Promise | Observable {
										const request = context.switchToHttp().getRequest();
										return validateRequest(request);
									  }
									}
									
					

Pipes

Pipes

							
		
									@Injectable()
									export class ValidationPipe implements PipeTransform {
									  transform(value: any, metadata: ArgumentMetadata) {
										return value;
									  }
									}
						
					

Interceptors

Interceptors


											  
					@Injectable()
					export class TransformInterceptor implements NestInterceptor {
					  intercept(context: ExecutionContext, next: CallHandler): Observable {
					    return next.handle().pipe(map(data => ({ data })));
					  }
					}

					/*

					{
					  "data": []
					}

					*/
							

Middleware

Middlewares

								
										
										@Module({
										  imports: [MeetupModule],
										})
										export class AppModule implements NestModule {
										  configure(consumer: MiddlewareConsumer) {
											consumer
											  .apply(LoggerMiddleware)
											  .forRoutes({ 
												  path: 'meetup', 
												  method: RequestMethod.GET 
												});
										  }
										}
							
						
									
		
										const app = await NestFactory.create(AppModule);
										app.use(logger);
										await app.listen(3000);
								
							

Exception Filter

Exception Filter

									
											

											@Catch(HttpException)
											export class HttpExceptionFilter implements ExceptionFilter {
											  catch(exception: HttpException, host: ArgumentsHost) {
												const ctx = host.switchToHttp();
												const response = ctx.getResponse();
												const request = ctx.getRequest();
												const status = exception.getStatus();
											
												response
												  .status(status)
												  .json({
													statusCode: status,
													timestamp: new Date().toISOString(),
													path: request.url,
												  });
											  }
											}
								
							

Let's try this shit

THE END

Vielen Dank für Ihre Aufmerksamkeit !



Presentation made by lovely reveal.js