Introduction
Creating a full-fledged application can be a daunting task, but with the right tools and frameworks, the process becomes much smoother. In this tutorial series, we will walk you through the process of building an Airbnb clone app using Nest.js, a popular Node.js framework for building scalable and maintainable server-side applications. To get started, we will guide you through setting up your development environment and creating a foundation for your project.
Technologies you will use
You will be using the following tools to build this application:
- NestJS - as the backend framework
- TypeORM - as the Object-Relational Mapper (ORM)
- MongoDB - as the database
- Postman - for Testing
TypeScript - as the programming language
Docker Desktop (Optional)
Prerequisites
Assumed knowledge
This is a beginner-friendly tutorial. However, this tutorial assumes:
- Basic knowledge of JavaScript or TypeScript (preferred)
- Basic knowledge of Express
Note: If you're not familiar with NestJS, you can quickly learn the basics by following the overview section in the NestJS docs.
Development environment
To follow along with this tutorial, you will be expected to:
- ... have Node.js installed.
- ... have Docker or Mongo atlas install
- ... have access to a Unix shell (like the terminal/shell in Linux and macOS) to run the commands provided in this series. (optional)
Generate the NestJS Project
Step 1: Setting Up the Nest CLI
The Nest CLI is a powerful tool that simplifies the process of creating and managing Nest.js applications. Before we dive into developing our Airbnb clone, let's make sure you have the Nest CLI installed. If you don't already have it, you can install it globally using NPM with the following command:
npm install -g @nestjs/cli
If you encounter any permissions issues during the installation, you can run the command with sudo and enter your password.
Step 2: Creating Your Project
With the Nest CLI installed, we can now generate our project. In this tutorial, we'll name our Airbnb clone app "Reservation." You're welcome to choose a different name if you prefer. To create your project, use the following command:
nest new reservation
then hit enter it will ask you the following questions
Which package manager would you ❤️ to use?
choose whatever you desire to use I'm using npm
This command will initialize your Nest.js application with a basic structure. We recommend using NPM as your package manager, as it helps optimize your project's dependencies.
Open the project in your preferred code editor (we recommend VSCode). You should see the following files:
reservation
├── node_modules
├── src
│ ├── app.controller.spec.ts
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── tsconfig.build.json
└── tsconfig.json
Most of the code you work on will reside in the src directory. The NestJS CLI has already created a few files for you. Some of the notable ones are:
src/app.module.ts:
The root module of the application.src/app.controller.ts:
A basic controller with a single route: /. This route will return a simple 'Hello World!' message.src/main.ts:
The entry point of the application. It will start the NestJS application.
Step 3: Verifying the Setup
You can start your project by using the following command:
npm run start:dev
This command will watch your files, automatically recompiling and reloading the server whenever you make a change. To verify the server is running, go to the URL http://localhost:3000/.
You should see an empty page with the message 'Hello World!'.
Note: You should keep the server running in the background as you go through this tutorial.
Step 4: Building a Common Library
The next step in our journey is to create a common library that all our microservices can use for shared operations, such as database access, authentication, and logging. This approach saves us from duplicating code across different microservices and simplifies maintenance.
The Nest CLI makes it easy to create this common library. We'll convert our standalone project into a monorepo, where we'll have a shared "common" module alongside each microservice app.
To create the common library, run the following command:
nest generate library common
once you hit enter you will see this promp:
What prefix would you like to use for the library (default: @app)?
just hit enter
This command will create a common library with the default prefix "app" and update several configuration files, including tsconfig.lib.json
, nest-cli.json
, and package.json.
it will generate the following file on the root of our directory
libs
├── common
├── src
├── common.module.ts
├── common.service.spec.ts
├── common.service.ts
├── index.ts
└── tsconfig.lib.json
since we are not going to use the common.module.ts
, common.service.ts
, and common.service.spec.ts
we are going to delete them later you will know why we did it for now just follow my instructions you can delete them manually or run this on your terminal:
rm libs/common/src/common.module.ts
rm libs/common/src/common.service.spec.ts
rm libs/common/src/common.service.ts
after that open the index.js inside the lib/src/common then delete these imports
export * from './common.module';
export * from './common.service';
Conclusion
In this tutorial, we've laid the foundation for our Airbnb clone app. We've set up our development environment, created a Nest.js project, and established a common library to share code across our microservices. This common library will become the backbone of our application, making it easier to develop and maintain. In the next part of the series, we'll delve deeper into building out our Airbnb clone app.
Next lessons Building a Common Module for Microservices with NestJS and MongoDB
Top comments (0)