The Beginners Guide: NestJS and How it is the Best JS Framework in the Market

Yannis Burghardt
4 min readMay 19, 2022

--

NestJS Logo

Introduction, What is NestJS?

NestJS is a JavaScript framework for building efficient and scalable server-side applications. It offers a rich set of features that can help developers create robust and maintainable code.

The framework enables developers to build easily microservices or all-in-one monolithic applications at no time. NestJS is an amazing framework that is easy to learn and use.

NestJS — The Foundation for High-Performance Applications

NestJS is perfect for creating large-scale applications. Its modular design makes it easy to break your application down into smaller, more manageable pieces. And because NestJS is based on Express and Node.js, you can be sure that your applications will be fast and reliable. If you do not want to use Express under the hood, you can swap it out with for example fastify.

3 Key Features of NestJS that Make it Ideal for Building a Real-World Production App

1. TypeScript support
This means you can build your application in TypeScript and use the benefits of Strict typing, annotations and more.
If you have not used TypeScript yet, this will improve your codebase drastically.

2. Time saving powered by Nest’s CLI
NestJS is bundled with a powerful Command Line Interface which enables you to generate code and files for you just by using short commands.

nest generate service database or nest g s database would generate you your database.service.ts in the blink of an eye.

3. NodeJS libraries
Since NestJS is built using NodeJS, we can use the entire catalog of NodeJS libraries. This gives NestJS great ascendancy.

NestJS Makes Node.js Development Easier and More Enjoyable for Developers

To show you, we gonna build a simple RESTful application.

First, we need to start by installing NestJS and creating our project.

$ npm i -g @nestjs/cli
$ nest new project-name
Project structure of a default NestJS project.
This is your default project structure.

The most relevant files you can see here are main.ts, app.controller.ts, app.module.ts, app.service.ts.

The main.ts is the entry-point of your NestJS application and is responsible for spinning up your application.

The app.module.ts is the default root module of your NestJS application. It is used to register all root controllers, providers as well as additional modules, which also can have their own controllers and providers.

Default app.service.ts

The app.service.ts is a default generated service which is not necessary in its default form. Services are supposed to be used for accessing data, and for executing tasks matching a specific topic (e.g. AuthService, UserService).

Default app.controller.ts

The app.controller.ts contains your application’s routes.
The default controller uses the app.service.ts, which gets Angular-like automatically injected, to get the value the HTTP-server should return if it receives a GET request on ‘/’.
If you request http://localhost:3000/ you should see the default message ‘’Hello World!” (The message is specified in app.service.ts).

Now let us add a second route!

Added the POST-route /message/send

This route should call the appService’s sendMessage(message) method and afterward respond with our specified object.

To test this we need to add a sendMessage(message) method to our AppService.

Now we can spin up the NestJS application by using the commands

$ yarn start:dev
$ npm run start:dev

It is time to test our RESTful endpoint, I will use Insomnia for that because I like it much more than Postman ¯\_(ツ)_/¯

Sent the request via Insomnia Client.

Et voilà! It works just fine.

Conclusion & Resources

As you can see it was very easy to build our very own first RESTful service. NestJS provides modules to connect to Databases, message-brokers & co. You can even use a pre-built SMTP module to send emails. If you would like some more content, maybe some in-depth tutorials, just let me know.

If you want to know something more, here you go:

--

--

Yannis Burghardt

Computer science student who loves to code and learn new things. Here I share my tips and tricks on how to be more productive and efficient with technology.