Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
341 views

Getting Started With NestJS - Better Programming - Medium

This document introduces NestJS, a framework built on Express that uses TypeScript and supports clean architecture. NestJS offers advantages like TypeScript support, code sharing capabilities, and following best practices from Angular. It can be installed via CLI and used to quickly generate a basic API project structure. Future articles will cover additional NestJS features and configuration.

Uploaded by

JamurPerak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
341 views

Getting Started With NestJS - Better Programming - Medium

This document introduces NestJS, a framework built on Express that uses TypeScript and supports clean architecture. NestJS offers advantages like TypeScript support, code sharing capabilities, and following best practices from Angular. It can be installed via CLI and used to quickly generate a basic API project structure. Future articles will cover additional NestJS features and configuration.

Uploaded by

JamurPerak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Getting Started With NestJS

Why you should consider NestJS for your next project

Santosh Yadav
Jan 2 · 4 min read

Pardon the interruption.


Sign up with Google

Sign up with Facebook

Sign up with email

Already have an account? Sign in

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.

Angular ecosystem vs. Node.js/Express.js ecosystem


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
Pardon the interruption.
and a well-defined architecture.

Code sharing: For a big enterprise


Signapplication, we may need to share the code
up with Google
across multiple apps or even APIs. In other programming languages, this can be
achieved using libraries. In ExpressJS,
Sign up we
withcan to create an npm module and make it
Facebook
available via Artifactory.
Sign up with email

. . .
Already have an account? Sign in

NestJS to the Rescue


To make Medium work, we log user data and share it with service providers. Click “Sign
NestJS is a framework that’s
Up” above written
to accept on topTerms
Medium’s of ExpressJS,
of Service &and it’s Policy.
Privacy written in TypeScript.
Let’s see some advantages:

TypeScript support: NestJS supports TypeScript, which makes me really


comfortable as I’ve been using TypeScript for a long time while working with
Angular. You have the option to choose JavaScript as well.

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.

. . .

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

Sign up with Facebook

Sign up with email

Already have an account? Sign in

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

You can also download the starter project


Sign up withfrom GitHub and use it.
Facebook

. . .
Sign up with email

Already have an account? Sign in


Creating our First API
Once the CLI is installed, run the following command to create a new application
To make Medium work, we log user data and share it with service providers. Click “Sign
called . We’ll
Up” above
shoppingAPI use the
to accept sameTerms
Medium’s API for our upcoming
of Service pieces.
& Privacy Policy.

nest new shoppingAPI

The CLI creates an app using Typescript as the default language. If you like
JavaScript as the language, you can run the following command:

nest new shoppingAPI -l JS

. . .
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

Sign up with Facebook


Conclusion
Sign
NestJS is really easy to start with, and if up
youwith email
have already used .Net, Java, or even
ExpressJS, most of the concepts are similar.
Already have an account? Sign in
It offers a CLI, and by using it, we can easily scaffold our app and focus more on our
code. For an enterprise application, it becomes really easy to split the code across
To make Medium work, we log user data and share it with service providers. Click “Sign
multiple modules using NestJS modules.
Up” above to accept Medium’s Terms of Service & Privacy Policy.

Special thanks to @MarkPieszak for review.

JavaScript Nestjs Typescript Programming Front End Development

About Help Legal

You might also like