Getting Started With NestJS - Better Programming - Medium
Getting Started With NestJS - Better Programming - Medium
Santosh Yadav
Jan 2 · 4 min read
To make Medium work, we log user data and share it with service providers. Click “Sign
Up” above to accept Medium’s Terms of Service & Privacy Policy.
This is the first part of a series on the Node.js framework known as NestJS. In this
piece, we’ll see why and how to use NestJS.
. . .
My Node.js Story
I’ve been using Express.js since 2017, and I love Express.js — it’s well designed, and we
can plug in any JavaScript library and use it.
I was a .Net developer for a long time and work with Angular now. I do miss a few
things while working with ExpressJS.
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
Pardon the interruption.
and a well-defined architecture.
. . .
Already have an account? Sign in
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.
Fastify support: NestJS uses ExpressJS as the default framework, but it also has
support for Fastify and can be easily configured.
. . .
Architecture
If I have to define the architecture of the API created using NestJS, this is how it looks
Pardon the interruption.
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.
Sign up with Google
To make Medium work, we log user data and share it with service providers. Click “Sign
Up” above to accept Medium’s Terms of Service & Privacy Policy.
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).
. . .
Installation
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:
Pardon the interruption.
npm install -g @nestjs/cli
Sign up with Google
. . .
Sign up with email
The CLI creates an app using Typescript as the default language. If you like
JavaScript as the language, you can run the following command:
. . .
Running and Testing the API
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. Pardon the interruption.
. . .
Sign up with Google