In this series of posts I will create an Authentication system with Passport using social auth and JWT. Let's use the combination of NestJs + Passport + Fastify to create a complete authentication system.
So in this first part, we start with a blank project and will use the Fastify Platform and understand how NestJs works.
What is NestJs?
NestJs is a framework for building efficient and scalable Node.js server-side applications. It uses robust HTTP Server frameworks like Express or Fastify. NestJs provides a level of abstraction above the common Node.js frameworks and exposes their APIs to the developer. This gives a great amount of freedom to use third-party modules.
Used for writing scalable, testable, and loosely coupled applications. It supports databases like PostgreSQL, MongoDB, MySQL.
Start Project
We will use the @nestjs/cli
to create a starter project and clear the files when we don't use them in our project. If you want to see the details, check the official documentation.
First, we install @nestjs/cli
globally in your computer and initialize a blank project. I recommend using yarn for installing packages, but you can choose a package manager your preference.
yarn global add @nestjs/cli
nest new nest-auth
And you access the folder and run in VSCode (or in another editor if you prefer).
cd nest-auth
code .
In the image below, you see the file structure, when we have an app module, controller, and service.
Type of blocks in NestJs
Modules: used to organize the code and split features into logical reusable units. Grouped TypeScript files are decorated with β@Moduleβ decorator which provides metadata that NestJs makes use of to organize the application structure.
Providers: also called services, which are designed to abstract any form of complexity and logic. Providers can be created and injected into controllers or other providers.
Controllers: responsible for handling incoming requests and returning appropriate responses to the client-side of the application (for example call to the API).
Configure Fastify Platform
In this project, I choose to use Fastify because it has a much better performance and functionalities than Express.
Fastify provides a good alternative framework for Nest because it solves design issues in a similar manner to Express. However, fastify is much faster than Express, achieving almost two times better benchmarks results. A fair question is why does Nest use Express as the default HTTP provider? The reason is that Express is widely-used, well-known, and has an enormous set of compatible middleware, which is available to Nest users out-of-the-box.
So let's configure that, in main.ts
we configure the platform if we use Fastify.
First, we need to install the required package:
yarn add @nestjs/platform-fastify
And in src/main.ts
you import FastifyAdapter
and NestFastifyApplication
for initialize that in NestFactory.create
.
// src/main.ts
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter()
);
await app.listen(3000);
}
bootstrap();
We don't need any more settings, now Fastify is configured and ready to use π
Run First Route
When making a project using @nestjs/cli
it creates an example route, so will use that route to test our configuration. In the next tutorials, I explain how routes work in NestJs. Now we want to know if Fastify is working.
Open app.controller
file, our see a default route using GET method in /
path when using the http://localhost:3000/
the server run getHello
function and return a response from AppService
// src/app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
The AppService
has a method called getHello
that returns a string Hello World
// src/app.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
So let's send a request using Postman for this route and see what returns
Good news its working π
Logger
If you want to get a log always that you send one request you can set this option inside Fastify Adapter
// src/main.ts
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ logger: true }),
);
await app.listen(3000);
}
bootstrap();
Here is an example if you use this option
Until Next Time
That is all we are going to cover in this article, we understood about NestJs and made an initial setup for our project to grow. In the next piece in the series, we'll configure the Database using Prisma, Docker, and Postgresql.
Thank you for reading!
Follow repository to consulting code
Top comments (0)