SECRET OF CSS

Navigating The NAP Stack: NestJS + Angular + PostgreSQL | by Ali Kamalizade | Aug, 2022


Developers love acronyms, so there is finally an acronym everyone can relate to

a seal sleeping on a bench
Photo by Jackman Chiu on Unsplash

When it comes to developing for the web, there are a lot of tech stacks out there. You might be familiar with acronyms like LAMP, MEAN, or MERN.

  • On the frontend side, Angular and React are common choices
  • On the backend side, Express.js is the most popular choice
  • On the database side, MongoDB and PostgreSQL are the most popular choices
  • TypeScript is becoming the prefered choice both in frontend and backend.

Personally, I am not that fond of Express. While it is easy to get started and there is a huge ecosystem around it, there are a couple of drawbacks:

  • Its development has stagnated for a while.
  • The lack of a proper out-of-the-box architecture makes projects look different, making onboarding and aligning on standards harder.
  • Express is rather barebones, so you need to install many third-party packages for common things needed for running microservices like authentication, caching, security and testing.
  • Testing can be more difficult than frameworks in other languages such as Java.

There is a fairly new kid on the (backend) block: NestJS. I know, naming can be quite confusing as there are similar-sounding projects like Next.js and Nuxt.js. There are a lot of reasons why Nest is gaining traction:

  • Nest strongly leverages TypeScript. While Express has TS support, it is still written in JS. Nest, on the other hand, is strongly typed out of the box and uses features like decorators.
  • Nest is a framework providing an out-of-the-box architecture. The architecture is heavily inspired by Angular: developers familiar with Angular will find their way around quickly. As Nest is closer to classic server-side frameworks like Spring Boot than Express, Java / C# developers will also understand Nest quickly.
  • Nest provides a lot of functionality out of the box or with the help of first-party packages. While Express is lightweight by itself, you will likely need to install a couple of third-party packages anyway.
  • The Nest CLI is a neat tool for increasing productivity and simplifying code generation, such as creating a new controller (another thing where the creators of Nest took inspiration from Angular)
  • You can still use Express (or Fastify) packages as Nest makes use of one of those frameworks under the hood
  • Nest has good support for testing. Mocking the return value of any injectable class is as simple and helpful as it is in Angular. Jest and supertest are already included by default.
  • The documentation is pretty extensive. Besides the basic Nest concepts, it covers topics like database, GraphQL, security, and performance. The documentation provides a set of recipes to start things like OpenAPI, health checks, and Prisma.

You probably noticed that a lot of the good points are because Nest took a lot of the good things in Angular to the backend world. This also means that you might not become a fan of Nest if you hate Angular passionately. You should still give Nest a go, though. Let’s have a look at the core concepts of any Nest application.

  • Controller: handle incoming requests and returning responses to the client.
  • Provider: usually a class that can be injected as a dependency.
  • Module: a class used to encapsulate a closely related set of capabilities. While you could squeeze everything into one module, it is better to split them.
  • Middleware: a function that is called before the route handler. Often used to make changes to the request and the response objects.
  • Pipe: a class that has a transform function. Pipes are useful for transforming input data to the desired form or for validation (throwing an exception when input data is incorrect)
  • Guard: a class determining whether a given request will be handled by the route handler or not, depending on certain conditions. It should throw an error if the user cannot access this endpoint.

Have a look at this basic Nest template to play around with Nest:

At work, this is our current favorite tech stack:

As developers love acronyms, and as I have to explain our tech stack in a few words, I was looking for a decent name for our tech stack. Something people will remember. Nobody will hopefully dispute the need for a NAP, so we should be good. Let me summarize the reasons why we went for this tech stack:

  • NestJS is a Node.js framework for building efficient, reliable, and scalable server-side applications.
  • Angular is a framework for creating web applications, focusing on productivity and enterprise readiness.
  • As Angular and Nest share many concepts, using TypeScript and a similar architecture makes knowledge transfer much easier, and developers quickly become productive.
  • PostgreSQL is a solid choice for primary storage. It provides the benefits of a relational database. And if you need to store data (as you would when using a non-relational database like MongoDB), then you can use the jsonb type.

Conclusion

Thanks for reading this article. What are your thoughts on Nest and this tech stack in general? Are you looking forward to your next NAP?

Let me know in the comments.



News Credit

%d bloggers like this: