Under the hood, Nest makes use of robust HTTP Server frameworks like Express (the default) and optionally can be configured to use Fastify as well!
Nest provides a level of abstraction above these common Node.js frameworks (Express/Fastify), but also exposes their APIs directly to the developer. This allows developers the freedom to use the myriad of third-party modules which are available for the underlying platform.
Nest provides an out-of-the-box application architecture which allows developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications. The architecture is heavily inspired by Angular.
To get started, you can either scaffold the project with the Nest CLI, or clone a starter project (both will produce the same outcome).
To scaffold the project with the Nest CLI, run the following commands. This will create a new project directory, and populate the directory with the initial core Nest files and supporting modules, creating a conventional base structure for your project. Creating a new project with the Nest CLI is recommended for first-time users. We'll continue with this approach in First Steps.
$ npm i -g @nestjs/cli $ nest new project-name
Alternatively, to install the TypeScript starter project with Git:
$ git clone https://github.com/nestjs/typescript-starter.git project $ cd project $ npm install $ npm run start
Open your browser and navigate to http://localhost:3000/.
You can also manually create a new project from scratch by installing the core and supporting files with npm (or yarn). In this case, of course, you'll be responsible for creating the project boilerplate files yourself.
$ npm i --save @nestjs/core @nestjs/common rxjs reflect-metadata
- CLI: Angular offers us a CLI to easily get started with a new project. Even in .Net, I can create an app using the CLI. Though there are many generators available, a CLI with ExpressJS would have been a great addition.
- Clean architecture: ExpressJS doesn’t come with any clean architecture defined, and, of course, it's not the purpose of ExpressJS — as you’re free to define your own architecture. But for an enterprise application, I prefer something that has a clean and well-defined architecture.
- Code sharing: For a big enterprise application, we may need to share the code across multiple apps or even APIs. In other programming languages, this can be achieved using libraries. In ExpressJS, we can to create an npm module and make it available via Artifactory.
NestJS is a framework that’s written on top of ExpressJS, and it’s written in TypeScript. Let’s see some advantages:
- Code sharing: NestJS supports creating libraries and applications using the CLI. It becomes really easy to share the code and becomes a great choice for enterprise applications.
- monorepo support: Angular supports monorepo, and starting with Version 6, NestJS comes with monorepo support.
- Learning path: Another thing I like about NestJS is if you’re coming from a .Net or Java background and have an idea about creating APIs, NestJS is easy to learn. Also, if you’re an Angular developer, you’ll feel at home since it follows the same modular pattern.
- Fastify support: NestJS uses ExpressJS as the default framework, but it also has support for Fastify and can be easily configured.
If I have to define the architecture of the API created using NestJS, this is how it looks like: We have a root module available, which will be used to configure database providers, to define the controller, to add middleware, to add the pipe and guards, and to provide services.
We can also have a module for each controller. We’ll see how to achieve that in upcoming pieces. Once our module receives a request, it’ll be redirected to the respective controller (which will handle the request). The service is optional, but we should try to use the service to follow the single responsibility principle (SRP).
Now, we have an idea of why we should use NestJS. Let’s see how to use it.
Before we can start using NestJS, we need to install the NestJS CLI. Run the following command to install the CLI globally:
npm install -g @nestjs/cli
- You can also download the starter project from GitHub and use it.
Once the CLI is installed, run the following command to create a new application called shoppingAPI. We’ll use the same API for our upcoming pieces.
nest new shoppingAPI
nest new shoppingAPI -l JS
Once a new project is created, we can use the following command to run the application.
cd shopping-API npm start
The app is configured to run on port 3000 by default. Once one controller is already defined, visit http://localhost:3000/, and you’ll get Hello World! as a response.
We have created our first app using NestJS with minimum configuration. In the next piece, we’ll go through the app structure and will configure our own controller to handle HTTP requests.
Standard mode vs. monorepo mode
NestJS supports two modes:
- Standard mode
- Monorepo mode
When we create a new application using nest new
A monorepo mode is where we have more than one application, generally, it's one app and one library, we already have app, let’s add a library by using the below command. It will add a new library project.
nest generate lib shoppingLib
Now, as we are in monorepo mode, rather than tsc, webpack will be used as a build tool. You can open the nest-cli.json and notice the below lines of code being added. compilerOptions has webpack value to true.
And after adding a library, the app structure will look like below:
Let’s go through some important files:
- tsconfig.json: The configuration file to be used while running tsc to transpile your files.
- nest-cli.json: The Nest CLI config file has a list of projects when we are in monorepo mode, and defines which build tool we are using webpack in monorepo and by default it’s tsc.
- main.ts: This is the entry file for our NestJS application and has a bootstrap() function to bootstrap our app. By default, the server runs on port 3000, you can change it and while using it in production, we can use an environment variable.
- app.module.ts: This is our root module which has registered all modules, controllers, services.
- app.controller.ts: Our application should have at least one controller defined and this is our default controller.
- app.service.ts: We can have multiple services in our application. This is a sample service added as a part of the workspace, this is optional.
- Libraries: Libraries can have reusable code like service, pipes, guards, or interceptors.
If you are building a full-stack application with Angular/React.js and want to use NestJS as a back end, you should consider Nx Dev Tools which provides a lot of features.