visit
nest g co name_of_your_controller
Now, you must be wondering – what if I get a chance to work with GraphQL?Well, NestJS comes up with all built-in modules for all the requirements of your . So, you can program using either a CodeFirst or a traditional ShcemaFirst technique.Moreover, NestJS also provides a built-in for testing purposes. This is quite easy to configure, that uses Jest and Supertest behind the scene.“Nest takes NodeJs servers to a whole new level of scalability.”Now, I am going to give you some of the best reasons that make Nest so great and why you should consider it for your next development project.
“Nest framework coding is similar to Angular back-end coding.”This is what an Angular developer said to me.“Apart from the concepts, it also works amazing with Angular Universal.” Furthermore, a developer added.Having a technical conversation with him and understanding the basic concepts, I could conclude that – as a developer, you can surely consider the NestJs framework for your back-end application. Moreover, if you have good expertise or knowledge of Angular, you must consider this as the learning curve becomes shallow.
“Nest is the new home for Angular developers!”
That’s where NestJs comes into the picture and stands alone among other NodeJs frameworks. Here, NestJs has an opinionated project setup, whereas other frameworks don’t have many architectural concepts. As a result, NestJs was born to solve architecture.
@Controller
: It handles incoming requests and returns responses to the client. Generally, many endpoints are encountered using HTTP requests.@Catch
: It’s also known as exception filters. It follows a simple method to handle concrete exceptions thrown by the controllers.@Injectable
: It’s used to inject in other classes.@Module
: It’s used to aggregate related components.Dependency Injection, an IOC design principle, is critical for connecting various layers and structuring our application.
So, now I am going to go through some of the advanced concepts, methods, and components which you can use to implement.Pipes: It's used for transforming and validating data.
Guards: Angular counterpart is one type of Guards. It lets you decide whether the navigation should be allowed to the specific route or not.
Middleware: Before a route-handler is executed, this function is used to run any code over the incoming requests.
Interceptors: It’s an AOP-oriented component used to bind extra logic before or after executing a controller method.
“The more knowledge you’ve, the easier things will be for you, irrespective of the technology you are working with.”So, now you have to install NodeJS in your system and run the given command:
npm i -g @nestjs/cli
Now, nest cli will be installed on your system for with the help of their client.
Now, let’s create a new project with the following command.nest new project-name
Here, we will create a project named nest-demo.Now, a scaffold will be created for your project. So, you have two options to choose from yarn and npm. Here, I am going to choose yarn.Let me give you an example here:apple-MacBook-Pro:code apple$ nest new nest –demo
We will scaffold your app in a few seconds..
CREATE /nest-demo/.prettierrc (51 bytes)
CREATE /nest-demo/README.md (3370 bytes)
CREATE /nest-demo/nest-cli.json (84 bytes)
CREATE /nest-demo/package.json (1721 bytes)
CREATE /nest-demo/tsconfig.build.json (97 bytes)
CREATE /nest-demo/tsconfig.json (336 bytes)
CREATE /nest-demo/tslint.json (426 bytes)
CREATE /nest-demo/src/app.controller.spec.ts (617 bytes)
CREATE /nest-demo/src/app.controller.ts (274 bytes)
CREATE /nest-demo/src/app.module.ts (249 bytes)
CREATE /nest-demo/src/app.service.ts (142 bytes)
CREATE /nest-demo/src/main.ts (208 bytes)
CREATE /nest-demo/test/app.e2e-spec.ts (561 bytes)
CREATE /nest-demo/test/jest-e2e.json (183 bytes)
? Which package manager would you ❤️ to use? Npm
❯❯❯ Installation in progress...
CREATE /nest-demo /package.json (1721 bytes)
CREATE /nest-demo/tsconfig.build.json (97 bytes)
CREATE /nest-demo/tsconfig.json (336 bytes)
CREATE /nest-demo/tslint.json (426 bytes)
CREATE /nest-demo/src/app.controller.spec.ts (617 bytes)
CREATE /nest-demo/src/app.controller.ts (274 bytes)
CREATE /nest-demo/src/app.module.ts (249 bytes)
CREATE /nest-demo/src/app.service.ts (142 bytes)
CREATE /nest-demo/src/main.ts (208 bytes)
CREATE /nest-demo/test/app.e2e-spec.ts (561 bytes)
CREATE /nest-demo/test/jest-e2e.json (183 bytes)
? Which package manager would you ❤️ to use? Npm
✓ Installation in progress...
Successfully created project nest-demo
Get started with the following commands:
$ cd nest-demo-pro
$ npm run start
app.controller.spec.ts
- It comes up with the unit tests for the controller.app.controller.ts
- It’s a minimal controller, which consists of a single route.app.module.ts
- It’s a root module for the application.app.service.ts
- It has a basic service with a single method.main.ts
- It’s an entry file of the application that utilizes the core NestFactory function. In a nutshell, it’s responsible for starting up the application instance.Now, let’s place all your source code in the src directory. As you can see that
main.ts
is the entry point in NestJS.Main.tsimport { NestFactory } from ‘@nestjs/core’;
import { AppModule } from ‘./app.module’;
async function bootstrap()
{
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
Now, you must be wondering why we have used
NestFactory.create()
?It’s used to create a new instance of the Nest Application. Also, the NestJS app’s root module, AppModule, is passed. Now, your NestJS app will be divided into various modules. Since we are creating a Nest app with a Root Module, each app will have a root module. Because
NestFactory.create
returns a promise; that’s why they utilized the await expression to resolve it.$ npm run start:dev
Now your application is ready to run on the localhost port 3000. Visit this URL in the web browser to view your endpoint. “Hello, World!” message will be displayed on your screen.
// app.controller.ts
@Controller()
export class AppController
{
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
Once the adaptor is generated, NestJs can function with any Node HTTP framework. However, it backs both express and fastify. So, based on the developer’s requirements, they can choose any of them.
Here, express or platform-express is the default choice for every developer. These frameworks are minimalist and have huge community support as well.
On the other side, we have got another option – platform-fastify. Fastify is a high-performance and efficient framework.
However, both the platforms come up with their own interface. These are used respectively as NestExpressApplication and NestFastifyApplication.
Anyway, let’s configure them in the given code with
app.create()
.const app = await NestFactory.create<NestExpressApplication>(AppModule);
The
NestExpressApplication
specific methods will be exposed as part of the app object using the execution mentioned above. We don't need to declare a type unless there’s a specific method to access it.