Nest Js

Should you opt Nest JS and why ?

Backend development with JavaScript makes a good developer experience. JavaScript has an apealing syntax, it is dynamically typed and introduces itself well for the beginner, however large scale application development with JavaScript can easily become a nightmare, and you have to be extra careful when using JavaScript. By now you’d have gotten some experience with using Typescript, TypeScript was introduced to help clean up our JavaScript and to make some sense out of our code. How is all this related to NestJS? You see, NestJs is a server side application development framework that’s built with TypeScript, we’ll come to NestJS later. Yes TypeScript greatly improves the quality of our applications and gives us more confidence. But project structure, software design approach is not for the feint of heart, if not carefully considered and thought through your application can easily become a mess of a code base.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

Have too much freedom and you might choke on it, most developers are not experienced with these concepts and even experienced developers will tell you it is not an easy or straight forward choice. What we want is to provide solutions but we also need solutions that can be easily managed and handed down to others. If you care about the work you do and how it affects others, then i believe you’d take responsibility for how you go about your job. Tools exists for a reason, and the perfect situation is a case where the right solution is applied with the right tool. When it comes to backend development with Typescript, there is hardly any tool better than NestJS.

When I first encountered and started using NestJS I thought it was an overkill, coming from a strong JavaScript background and having developed backend applications with JavaScript, then progressively using TypeScript for backend development, I did not see the need to start using a framework to do the things which I could do without one. Nevertheless, being an enthusiast I still decided to get my hands dirty with the framework. After almost 10 months of using NestJS, I can say that my first impression of the framework was bloated and I’ve grown to love the framework, today I want to discuss with you five things I’ve come to love about the framework.

Easy to get started with

NestJS is very easy to get started with as a beginner, There is a small learning curve, especially if you are already familiar with Typescript. It presents itself in a very simple manner and it is straight forward to start using it. The official documentation for NestJS is well thought out and it is very easy to understand. It wasn’t much of a stress for me to find my way around the framework. It took me just about two weeks to build my first backend app which was powered with NestJS and that was coming from not using the framework at all. I was learning and building and I was having a good developer experience, I don’t have all the time to spend learning and using the framework but it actually helps me to get things done faster so I can focus on the other stuffs that need my attention. Which leads us to the next point.

Example intergration of Nest js and Mongo DB

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://localhost:27017',{dbName: 'studentdb'}),
  ],
})
export class AppModule {}

Fast Development Speed

Development with NestJS is fast paced, i managed to get a dummy backend in place within just a weeks work and i didn’t spend all day everyday doing it. The awesome TypeScript support, coupled with the smart approach chosen by the developers took away many of the stuffs which i had to handle when developing applications without NestJS. You’d agree with me that using the code>@Req() when handling the request object is so much easier than having to accept both parameters when you only need one. This approach helps to reduce the amount of unnecessary code duplication, many a time in the past I would have to pass in the Request and the Response object.

Application Structure

NestJS has a strong opinion about how we should go about building our applications, it enforces a certain application structure. The familiar MVC approach is followed suit in NestJS, we have models that can be composed of a number of different types of databases, we then define controllers which will handle user request, although I don’t use this feature as much, NestJS can also render a view for you. This eliminates the need for having to worry about what architecture your application is going to adopt. NestJS also addresses the problem of file structure, but that is entirely down to you to decide and personally I prefer to adopt my own folder structure. But if you are not really experienced with that then you could just stick to what NestJs provides for you out of the box.

Easy Integration With Other Modules

NestJS has integrations for most of the major libraries which we use for server side application development with JavaScript, most of the cases were thought in advance and pre-built solutions already exists. NestJS can be easily coupled with Axios to enable server side data fetching, it can also be coupled to almost any database; MongoDB and MongooseTypeORM and MySQL, just to name a few. I did not have trouble integrating multer for handling file uploads with NestJS, like I’ve said so many times already, All this was carefully thought about. If it isn’t already available then I’ve also discovered that it is quite easy to mix the both of them, you would find it easy to use your favorite library with NestJS.

TypeScript Support and Dependency Injection

NestJS has out of the box support for Typescript since it is built with TypeScript, this offers so much advantage because you already have a backend project setup with TypeScript for you. You can keep your focus on what you should, development with Typescript gives developers more confidence in the quality of code they ship out and less time is and spent trying to fix type error bugs. NestJS also makes use of dependency injection (DI) as it is found in Angular, this is because NestJS borrows a lot of concept from Angular and DI is one of them. It allows us to scope the providers and controllers in our code to a module that is responsible for managing those dependencies, as a direct consequence of this approach it eliminates the need for any dead code and only dependencies that we use are actually bundled with our application.

Supports Rest and GraphQL

Rest API Nest JS

From a single NestJS code base, our application can be adapted to accommodate for a REST API or a GraphQL API easily depending on which ever approach makes sense to you. By default the standard approach is REST, but you could easily switch to a GraphQL API and there are two approach to implementing a GraphQL API using NestJS. I like how it lets you choose the kind of approach you want from the same codebase without having to worry much about configuration or integration, again keeping your focus on developing your application.

These are six reasons why we think that using NestJS for your next backend project actually makes a ton of sense, have you already used NestJS before now? Well then what are your thoughts and experience with using NestJS, what are the benefits of using NestJS that i left out of this one? Feel free to drop your comments and as always, hope you enjoyed this and until the next one.

Back to top button
Close

Adblock Detected

Please disable your adBlocker. we depend on Ads to fund this website. Please support us by whitelisting us. We promise CLEAN ADS ONLY