1. Understand NestJS with ExpressJS

Mohammed El Khaira
Tales of Libeo
Published in
3 min readMay 27, 2021
Nest looking at Express

Today I wanted to start sharing with you a series of articles focused on the NestJS framework. Before joining Libeo, I used to work with the ExpressJS framework. I’ve since then been introduced to NestJS which I have never heard of before. In order to understand the way this new framework works under the hood, I started working on a small app using NestJS alongside ExpressJS. This small project, presented over the course of multiple articles, will make use of Typescript, ESLint, Prettier, TypeORM, Apollo, Jest and even some React. I hope you will enjoy it and benefit from it as much as I will!

Some context

In order to start this article, I have to go through a better explanation of NestJS. It is a framework for building efficient and well-structured Node projects. Invented by Kamil Myśliwiec, it facilitates the development of scalable server-side applications. As a matter of fact, it makes use of the exact same ExpressJS framework we will be using in these articles.

Its main purpose is to answer a simple question: how can I structure my Node app ? By combining Object Oriented Programming, Functional Programming and Functional Reactive Programming, NestJS is built to provide resourceful tools. It also supports TypeScript which, as a JS developer, I cannot refrain from using.

The first step of our journey is to build a basic NestJS app and understand its power by comparing it to a basic ExpressJS one

Let’s dive in

Express app

First of all, install node and npm(we recommend you to use NVM) then install yarn.

Create a new repository: mkdir express-app && cd express-app.

Initialise the package.json file: yarn init -y

Then install the express package : yarn add express

Create a folder named src then an index.js file: mkdir src && touch src/index.js.

Open the file and add the following JS code:

Now you can run your app with node index.js. But it’s better to define a start script to launch the app. Open package.json and add the following “scripts” object. Also, you will have to update the “main” property value by giving the path to your index.js file:

Finally, run the app with yarn start. Go to http://localhost:8080 and Tada! Your express app is running! 🥳🎊

Nest app

Install the Nest CLI with yarn: yarn global add @nestjs/cli

Then initialise your project with nest new nest-app. Choose yarn as your package manager.

With the Nest CLI, your project comes with a default structure, TypeScript, ESLint, Prettier, Jest and Git initialised. Also, it has a pretty neat modular structure:

If you open package.json, you will see all the dependencies already installed and all the scripts available:

I recommend you to open the main.ts file and change the default app port to 9000. Then run your app with yarn start. Go to http://localhost:9000 and Voila! Your nest app is running! 🤩

Conclusion

It’s already over? Don’t worry, we will explore how this Nest boilerplate works and how we can pimp the Express one to get a similar structure. Stay tuned!

--

--