MEAN Stack with NestJS and Swagger

Chau Tran
3 min readJun 22, 2018

--

Image from https://matwrites.com/wp-content/uploads/2018/01/GLOBAL-BIZ-SUMMIT.png

Hey, what’s going on everyone? My name is Chau Tran and I am a Web Developer. Since I started, I’ve been “working” on the MEAN Stack.

Angular on the frontend is awesome. Though this might get a lot of hates but I do like its “opinionated” approach and Angular does it tremendously well. However, what’s missing is a great framework on the backend to accommodate the Angular frontend-Typescript sort-of thing. On that note, I’ve looked into Loopback but v4 is still in development. Ironically, I’ve discovered something called NestJS and it looks…..promising. It has everything I need it and what else? It takes on Angular’s Module/Component Approach. Then, I picked up NestJS (https://nestjs.com/) before I knew it.

After a while of playing and working with NestJS, I am not regretting at all. In addition, I feel like I need to spread this NodeJS framework around to share to everyone. It is a great framework, especially for the MEAN stack. Hence, I am working on a Youtube series called: MEAN Stack with NestJS and Swagger (https://www.youtube.com/playlist?list=PLVmX3uPQtp3uRu30YKw0v2VIPgZo45FUD). Oh, I didn’t mention Nest does include a SwaggerModule as well.

Technologies used/mentioned in the series:

  • NodeJS/Express
  • NestJS
  • Mongoose/Typegoose
  • JSONWebToken
  • Passport
  • Swagger/NSwag
  • Angular

In the series, I am going to talk about:

  • Part 1: NestJS Introduction and Visual Studio Code setup for development. (released)
  • Part 2: We Shared a bit of Swag and a bit of Goose. Just a “pun” title for the video, I thought it’s funny. In part 2, I went over SharedModule, SwaggerModule, and MongooseModule on how I set those up, what’s included. In SharedModule, I implemented a ConfigurationService to handle our application variables and a proof-of-concept MapperService utilizing automapper-ts, we’ll see how this Mapper goes. Moreover, I also implemented a BaseModel and BaseService for Abstraction/Inheritance on the basic operations like: find, findOne, update, create, delete. (released)
  • Part 3: Initially, I planned to start implementing Authentication with UserModule and AuthService utilizing JWT and passport. NestJS provides a @nestjs/passport package that can help making things a tad easier. But, I discovered a package called typegoose through my boss which allows me to create mongoose’s Schemas using Classes and Decorators. I tried it out afterwards and decided to go back and integrate typegoose with BaseModel and BaseService. (released)
  • Part 4: Todos CRUD and Guards (released)
  • Part 5: Angular Frontend and how it communicates with the Backend with NSwag

Through this series, I hope people will give NestJS a chance. The framework has already had 6600 stars on Github so far given the short amount of time since its first version was released. If you’re already using the MEAN stack, or you’re familiar with Angular, or you’re looking for a good and stable NodeJS backend framework to experiment, please give NestJS a “test-drive”. Again, here’s the link to the series: https://www.youtube.com/playlist?list=PLVmX3uPQtp3uRu30YKw0v2VIPgZo45FUD

Amend to Mistakes:

  1. In Part 3, I made a mistake where I forgot to default the UserRole to a role. Hence, the role was never set for a User and this resulted in a broken JWT without a role. The following snippet shows the fix:

2. Also in part 3, I made another mistake which I forgot to add a @Catch decorator to the HttpExceptionFilter so the filter failed to catch the HttpExceptions thrown from the services and controllers :

3. This is not a mistake, but rather an enhancement. While working on the . Todo CRUD for part 4, I realized the updatedAt field is never updated because we never set the model to update the updatedAt field. Here’s an addition to our BaseModel to actually implement such feature. Typegoose provides a @pre decorator for pre hook in Mongoose. We’re going to utilize and couple that with a generic type <T> on our BaseModel so that all of our models will have the same feature:

You’ll also need to update the declaration of User in user.model.ts to:

export class User extends BaseModel<User> {}

PS: I’ll keep this post updated with new videos as well as news on performance, strengths and weaknesses as I work more on it. My company is also using NestJS for production in two of our projects.

Updated with Github link: https://github.com/nartc/nest-mean

Updated Github Repo with Client project and Docker support. More written tutorial coming up soon!

--

--