Nest.js for beginners

Chamuditha Kekulawala
4 min readJun 19, 2024

--

Nest.js is a Node.js framework for building scalable, server-side applications with TypeScript.

Why Nest.js?

As you probably already know, a popular Node.js framework is Express. By design it’s meant to be very minimalist. It introduces some features that let’s you build server-side applications, but the overall architecture of your backend is upto you.

If you take a look at the Express documentation they give you some features to do routing, middleware, some error handling, and that’s pretty much it. This lack of architecture means that it’s extremely flexible; you can pretty much do whatever you want with Express.

However as your project or team grows you’ll find that things can very easily turn into spaghetti 🍝. You really need conventions and structure, which is very important especially when you need to start integrating with things like GraphQL or REST or Swagger.

Now Nest comes into the picture saying: “Hey, we’ll take care of the architecture of your server-side application for you. And the best part? You still get to use Express under the hood!”

How Nest.js works

Nest.js provides a suite of tools that leverage express (and even Fastify) to facilitate rapid development and predictable, readable code. It supports REST and GraphQL APIs out of the box or you might use it to build a full stack application using the Model-View-Controller (MVC) design pattern, similar to frameworks like Laravel or Ruby on Rails

It also contains a ton of built-in modules to work with databases, handle security, implement streaming and anything else you can imagine doing in a server- side application

Nest.js has some great documentation. They provide overviews of all of the core concepts, but on top of that you’ll see that they have guidelines on how to do a lot of other things like testing, database connections, task scheduling, queues, events, WebSockets, GraphQL, etc.

It’s also very big on TypeScript, combining elements form OOP and FP. You’ll see that there’s a big emphasis on decorators, which you might be familiar with if you’ve used the Springboot framework.

The Nest CLI

Nest has its own very powerful Command Line Tool and you can scaffold out a new project with the nest new command:

npm i -g @nestjs/cli
nest new project-name

This installs the Nest CLI globally and provides a code base pre-configured with Jest for testing and set up with typescript. Now you can get started with Nest.js!

Folder structure

In the project-name directory, node modules and a few other boilerplate files will be installed, and a src/ directory will be created and populated with several core files:

src
- app.controller.spec.ts // A basic controller with a single route.
- app.controller.ts // The unit tests for the controller.
- app.module.ts // The root module of the application.
- app.service.ts // A basic service with a single method.
- main.ts // The entry file of the application

The main.ts includes an async function, which will bootstrap our application:

main.ts file

The main.ts includes the async function NestFactory to create a Nest application instance, which will bootstrap our application:

Platform

Nest is able to work with any Node HTTP framework once an adapter is created. There are two HTTP platforms supported out-of-the-box: express and fastify. You can choose the one that best suits your needs.

If you check your package.json file, under dependencies you will see that the @nestjs/platform-express package is used by default.

Running the application

You can run the following command in your terminal to start the application listening for inbound HTTP requests:

npm run start

nce the application is running, open your browser and navigate to http://localhost:3000/. You should see the Hello World! message.

To watch for changes in your files, you can run the following command to start the application:

npm run start:dev

This command will watch your files, automatically recompiling and reloading the server.

Now you can get started with Nest.js! Thanks for reading 🎉

--

--