Js Mobile Dev About Courses Articles Youtube Services Contact

Install NestJS and create your first application using the @nestjs/cli

Libraries and Versions

Backend: NestJS v8.0.0

To create a new NestJS application, we first need to make sure we have the @nestjs/cli installed.

To do that, open your terminal and type:

npm install -g @nestjs/cli

Once the CLI is installed, you can type:

nest new myFirstApp

This command will create a new NestJS application with the name myFirstApp.

NestJS Folder Structure

In there, you'll find all of the files that the CLI generates, for example:

  • Your typescript configuration.
  • Your package files.
  • Linter configurations.
  • And several folders.

The bulk of your app's code will live in the src folder.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

Our entire NestJS application starts with the main.ts file, creates our application, and initializes our AppModule.

Our app.module.ts file is the main module of our application.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

In there, we'll declare every other module, service, controller, pretty much everything we need for our application to work.

If this looks familiar to you is because Angular heavily inspires the creators of the framework.

The other essential file you'll find here is the app.controller.ts. This is a regular class with the special @Controller() decorator.

This is where our application handles every individual request, and it looks like this:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller('api')
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): { name: string } {
    return this.appService.getHello();
  }
}

To test it, you can run your application with npm run start:dev. It will start a dev server on port 3000.

Then you can make a GET request to http://localhost:3000, and you'll get Hello World in return.

NestJS Folder Structure

As the GET request, you can also make any other type of HTTP request. For example, you can set a method for POST requests.

import { Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Post()
  changeHello() {
    return 'Not Hello World';
  }
}

Now, if you make a POST request to http://localhost:3000, you'll get the Not Hello World response back.

There are several things we can do here. For example, our AppController is set up for the root route of our domain, but we can add the route api for example:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller('api')
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

Now, if you make a GET request to http://localhost:3000, you'll get a 404 error because the route doesn't exist anymore, but if you call http://localhost:3000/api, you'll get the methods.

You can also do it individually:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('hi')
  getHello(): string {
    return this.appService.getHello();
  }
}

Changing back the @Controller() and adding a route on the @Get('hi') decorator, all of the methods will remain the same, but for the GET request, you'll get a 404 if you call http://localhost:3000 and instead you'll need to call http://localhost:3000/hi.

You get the idea :)

What do you think of NestJS? Is it something you see yourself using over express or even Firebase? Shoot me a message at @javebratt


There's more where that came from!

I email every Tuesday with the latest articles and insights about building scalable apps with Ionic, Firebase, nodejs, and Angular 🚀.

Drop your email in the box below and I'll send new guides straight to your inbox!

    I don't do the spam thing, you can one-click unsubscribe at any time