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.
In this set of articles, you’ll learn the core fundamentals of Nest. To get familiar with the essential building blocks of Nest applications, we’ll build a basic CRUD application with features that cover a lot of ground at an introductory level.
To create a Nest application instance, we use the core
NestFactory exposes a few static methods that allow creating an application instance. The
create() method returns an application object, which fulfills the
INestApplication interface. This object provides a set of methods which are described in the coming chapters. In the
main.ts example above, we simply start up our HTTP listener, which lets the application await inbound HTTP requests.
Note that a project scaffolded with the Nest CLI creates an initial project structure that encourages developers to follow the convention of keeping each module in its own dedicated directory.
Nest aims to be a platform-agnostic framework. Platform independence makes it possible to create reusable logical parts that developers can take advantage of across several different types of applications. Technically, 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.
Express is a well-known minimalist web framework for node. It’s a battle tested, production-ready library with lots of resources implemented by the community. The
@nestjs/platform-express package is used by default. Many users are well served with Express, and need take no action to enable it.
Fastify is a high performance and low overhead framework highly focused on providing maximum efficiency and speed. Read how to use it here.
Whichever platform is used, it exposes its own application interface. These are seen respectively as
When you pass a type to the
NestFactory.create() method, as in the example below, the
app object will have methods available exclusively for that specific platform. Note, however, you don’t need to specify a type unless you actually want to access the underlying platform API.
A controller’s purpose is to receive specific requests for the application. The routing mechanism controls which controller receives which requests. Frequently, each controller has more than one route, and different routes can perform different actions.
In order to create a basic controller, we use classes and decorators. Decorators associate classes with required metadata and enable Nest to create a routing map (tie requests to the corresponding controllers).
In the following example we’ll use the
@Controller() decorator, which is required to define a basic controller. We’ll specify an optional route path prefix of
cats. Using a path prefix in a
@Controller() decorator allows us to easily group a set of related routes, and minimize repetitive code. For example, we may choose to group a set of routes that manage interactions with a customer entity under the route
/customers. In that case, we could specify the path prefix
customers in the
@Controller() decorator so that we don’t have to repeat that portion of the path for each route in the file.
The above is a brief about Nest.js. Watch this space for more updates on the latest trends in Technology.