NestJS: Introduction

alvarotorresc

Alvaro Torres Carrasco

Posted on June 14, 2020

NestJS: Introduction

This is the first post about NestJS. The introduction

NestJS is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with and fully supports TypeScript (yet still enables developers to code in pure JavaScript) and combines elements of Object Oriented Programming, Functional Programming, and Functional Reactive Programming.

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.

Prerequisites

You have to make sure that Node.js (>= 10.13.0) is installed on your operating system.

Language

NestJS is built with and fully supports TypeScript. However, Nest is compatible with both TypeScript and pure JavaScript. This framework takes advantage of the latest language features, so to use it with vanilla JavaScript we need a Babel compiler.

Why NestJS?

The main problem with other javascript frameworks is the structure of the project made with them. The majority of them has many powerful features and solutions but they dont have a easy testeable structure or a heavily convoluted file tree. However, 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.

Strengths

[X] Typescript and Javascript fully support
[X] Powerful CLI
[X] Decorators
[X] Extensible, Versatile, Progressive
[X] Open Source
[X] GraphQL Friendly
[X] WebSockets integrated

Powerful CLI

The Nest CLI is a command-line interface tool that helps you to initialize, develop, and maintain your Nest applications. It assists in multiple ways, including scaffolding the project, serving it in development mode, and building and bundling the application for production distribution. It embodies best-practice architectural patterns to encourage well-structured apps.

All nest commands follow the same format:

nest commandOrAlias requiredArg [optionalArg] [options]

Command overview
Alt Text

Platform

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.

Alt Text

Installation

GitHub logo nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) 🚀

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Support us

Description

Nest is a framework for building efficient, scalable Node.js server-side applications. It uses modern JavaScript, is built with TypeScript (preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).

Under the hood, Nest makes use of Express, but also, provides compatibility with a wide range of other libraries, like e.g. Fastify, allowing for easy use of the myriad third-party plugins which are available.

Philosophy

In recent years, thanks to Node.js, JavaScript has become the “lingua franca” of the web for both front and backend applications, giving rise to awesome projects like Angular, React and Vue which improve developer productivity and enable the construction of fast, testable, extensible frontend applications. However, on the server-side, while there are a lot of superb libraries, helpers and tools…

Install via npm:

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

Install via yarn:

yarn add @nestjs/cli
nest new project-name

Useful Templates

Made with Docker and Prisma:

Nest.js Backend Template

Getting started

  1. Clone this repository and open it
$ git clone https://github.com/z1-lab/nestjs-backend-template
$ cd nestjs-backend-template
  1. Install dependencies
$ yarn
  1. Create a copy of the .env.development file and call it .env. Update the environment variables to match your current environment.
$ cp .env.development .env
  1. Launch a PostgreSQL database with docker, it will use the port defined in the DB_PORT var.
$ docker-compose up
  1. Generate the prisma client
$ yarn prisma generate
  1. Create the database model
$ yarn prisma migrate up --experimental
  1. Launch the dev mode
$ yarn dev
  1. If you're going to edit the GraphQL schema, run the following command to regenerate the TypeScript types.
$ yarn gql:typings --watch

Scripts

  • yarn dev. Runs the project in dev mode, which means that it won't check types and will restart with every change you make.
  • yarn build. Compiles the project to the ./dist folder.
  • yarn typecheck

Typescript nestjs starter:

GitHub logo nestjs / typescript-starter

Nest framework TypeScript starter ☕

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.



NPM Version
Package License
NPM Downloads
Travis
Linux
Coverage
Discord
Backers on Open Collective
Sponsors on Open Collective


Description

Nest framework TypeScript starter repository.

Installation

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.


Follow me on Twitter if you want to know about my future articles, projects or whatever

💖 💪 🙅 🚩
alvarotorresc
Alvaro Torres Carrasco

Posted on June 14, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related