A progressive Node.js framework for building efficient, reliable and scalable server-side applications. **Official website: https://nestjs.com/
🔍 What do you find here?
❯ npm i -g @nestjs/cli
❯ nest new project-name
<aside> 👉 controllers: responsible for handling incoming requests and returning responses to the client.
providers: can be injected as a dependency; this means objects can create various relationships with each other, and the function of "wiring up" these objects can largely be delegated to the Nest runtime system.
modules: is a class annotated with a @Module()
decorator. The @Module()
decorator provides metadata that Nest makes use of to organize the application structure.
middleware: is a function which is called before the route handler. Middleware functions have access to the request and response objects, and the next()
middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next
exception filters: is responsible for processing all unhandled exceptions across an application. When an exception is not handled by your application code, it is caught by this layer, which then automatically sends an appropriate user-friendly response.
pipe: is a class annotated with the @Injectable()
decorator, which implements the PipeTransform
interface.
guard: is a class annotated with the @Injectable()
decorator, which implements the CanActivate
interface.
interceptor: is a class annotated with the @Injectable()
decorator and implements the NestInterceptor
interface.
custom route decorators: is an expression which returns a function and can take a target, name and property descriptor as arguments. You apply it by prefixing the decorator with an @
character and placing this at the very top of what you are trying to decorate. Decorators can be defined for either a class, a method or a property.
</aside>
import {
Body,
ConflictException,
Controller,
HttpCode,
Post,
} from '@nestjs/common'
import { PrismaService } from 'src/prisma/prisma.service'
@Controller('/accounts')
export class CreateAccountController {
constructor(private prisma: PrismaService) {}
@Post()
@HttpCode(201)
async handle(@Body() body: any) {
const { name, email, password } = body
const userWithSameEmail = await this.prisma.user.findUnique({
where: {
email,
},
})
if (userWithSameEmail) {
throw new ConflictException(
'User with same e-mail address already exists.',
)
}
await this.prisma.user.create({
data: {
name,
email,
password,
},
})
}
}
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
import { PrismaClient } from '@prisma/client'
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
constructor() {
super({
log: ['warn', 'error'],
})
}
onModuleInit() {
return this.$connect()
}
onModuleDestroy() {
return this.$disconnect()
}
}
import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { PrismaService } from './prisma/prisma.service'
import { envSchema } from 'src/env'
import { AuthModule } from './auth/auth.module'
import { CreateAccountController } from './controllers/create-account.controller'
import { AuthenticateController } from './controllers/authenticate.controller'
import { CreateQuestionController } from './controllers/create-question.controller'
@Module({
imports: [
ConfigModule.forRoot({
validate: (env) => envSchema.parse(env),
isGlobal: true,
}),
AuthModule,
],
controllers: [
CreateAccountController,
AuthenticateController,
CreateQuestionController,
],
providers: [PrismaService],
})
export class AppModule {}