Creating your first NestJS application

Photo by Sereja Ris on Unsplash

What’s NestJS?

When to use NestJS?

Why use NestJS?

  • Type checking: with Typescript we exploit all the power of javascript without running into type problems.
  • Modular: through the division into modules we can isolate our components based on the domain boundary.
  • Dependency Injection: thanks to the DI container we are able to write decoupled and testable code.
  • Adapters: Nest integrates all the most used and community tested javascript / typescript libraries and provides all the tools developers need to contribute to the collection.
  • Angular Like: the familiar structure allows us to easily switch from frontend to backend without having to change the development approach every time.
  • CLI: we can develop our functionality without having to worry about the initial boilerplate.

Let’s start writing code

  • retrieve the list of all todos
  • retrieve a single todo
  • insert a todo
  • edit a todo
  • delete a todo
  • [GET] todos
  • [GET] todos/{id}
  • [POST] todos
  • [PUT] todos/{id}
  • [DELETE] todos/{id}
Ehm 😅

step 0: Setup

npm i -g @nestjs/cli
nest new TodoBackend
NestJS bootstrap logs

step 1: Module

  • It’s not a typical case and consequently difficult to apply in a real context.
  • We would not be decoupled, thus losing the maintainability and portability of our code.
nest generate module todo

step 2: Entity

nest generate class todo/entities/Todo --no-spec

step 3: Repository

npm i @nestjs/typeorm typeorm sqlite3

step 4: DTO

step 5: Service

nest generate service todo/services/todo
nest generate service todo/services/TodoMapper

step 6: Controller

nest generate controller todo/controllers/todo
@Post()
@UseInterceptors(ClassSerializerInterceptor)
public add(@Body() todo: AddTodoDto): Promise<TodoDto> {

step 7: Validation

npm i class-transformer class-validator

step 8: Run!

npm run start
npm run start:debug

step 9: Enable CORS

step 10: Test

Conclusion

--

--

--

Fullstack Developer | Based in Florence | https://github.com/KernelPanic92 | https://iacopociao.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Getting started with the protractor

Writing a Portable Vue.js Logic with Mixins — Noty-based Notification System

JavaScript Regular Expressions (Regex) — Beginner to Expert

JavaScript Regular Expressions (Regex) - Beginner to Expert

How to use AWS transcribe javascript SDK with angular

Final Project at Flatiron School

Top 7 React Libraries Every JavaScript Professional Should Know

Drawer Navigation for React Native with React Native Elements

Beautiful Javascript

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Iacopo Ciao

Iacopo Ciao

Fullstack Developer | Based in Florence | https://github.com/KernelPanic92 | https://iacopociao.com

More from Medium

NPM Workspaces & Microservices

Difference Between Middleware, Interceptor, and Filter in NestJS (2022)

Working with Multiple Node JS Versions On A Single Environment

Nx NestJs — How to autogenerate OpenApi/Swagger specs