In this article, you’ll learn how to setup a new NestJS project and create a first api
Table of contents
Open Table of contents
Intro
Nest (NestJS) is a framework for building efficient, scalable NodeJS server-side applications. 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. It is best suited for REST APIs as it is really quick to get a REST API running with NestJS.
1. Prepare the workspace
The official guide suggests to install the cli globally and create a new project from it
$ npm i -g @nestjs/cli
$ nest new project-name
The second method, which is what we consider in this post, is to use the template ready-to-go available in Github from this repository into a new one or just clone in your local machine
$ git clone https://github.com/BogdanStanciu/nestjs.git
2. Install dependencies
Install the dependencies of Nest with
$ npm install
Moreover it is necessary to have a mongo database where to save the data, you can install directly on your local machine or by running it into a docker
docker-compose.yml
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
volumes:
- mongo-volume:/data/db
ports:
- 27017:27017
restart: always
networks:
local_network:
aliases:
- mongo
volumes:
mongo-volume:
3. Configs
In the root of the project, there is an .env
file containing the configurations for the
application. Here you have to enter the various parameters for the connection to the database and the port where
will run the application
BACKEND_PORT=3000
DB_HOST=localhost
DB_PORT=27017
DB_NAME=app
These configurations are used at application bootstrap to initialize the mongo module which creates a connection to the database.
@Module({
imports: [
MongooseModule.forRoot(
`mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`
),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
4. Start the app
Once the dependencies are installed and the envs configured, to start the application run the following command:
$ npm run start:dev
This command will watch your files, automatically recompiling and reloading the server.
[3:42:49 PM] Starting compilation in watch mode...
[3:42:51 PM] Found 0 errors. Watching for file changes.
[Nest] 17422 - 02/21/2023, 3:42:51 PM [NestFactory] Starting Nest application...
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] MongooseModule dependencies initialized +28ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] PassportModule dependencies initialized +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] MongooseCoreModule dependencies initialized +2ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] JwtModule dependencies initialized +1ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] MongooseModule dependencies initialized +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] AuthModule dependencies initialized +1ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [InstanceLoader] UserModule dependencies initialized +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RoutesResolver] AppController {}: +73ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RouterExplorer] Mapped {, GET} route +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RoutesResolver] UserController {/user}: +1ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RouterExplorer] Mapped {/user, POST} route +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RouterExplorer] Mapped {/user, GET} route +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RouterExplorer] Mapped {/user/:id, DELETE} route +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RoutesResolver] AuthController {/auth}: +0ms
[Nest] 17422 - 02/21/2023, 3:42:51 PM [RouterExplorer] Mapped {/auth/login, POST} route +1ms
Now the application is listening for connection on the port 3000, to test it, just curl che host
$ curl http://localhost:3000/
Hello, World !
Conclusion
We have seen how to set up a new project starting from the nestjs template, in the next posts we will see how to deploy our application with docker.