Record some learning and sharing of using Nest. js to build personal blog. Through using and taking notes, I hope I can learn more solid and master knowledge more skillfully. Today, do the construction of the project first, access to the application is successful.

start

Building a Nest. js project is very simple and can be done easily by NPM, as follows:

  • npmInstallation:

    npm i -g @nestjs/cli
  • Installation items:

    nest new project-name

With nestJS scaffolding installed, we can create a new project with the following directory structure:

The directory structure

As you can see in the figure above, the initial NestJS project directory structure is very simple, with only five files under SRC. Let’s see what happens.

main.ts

First look at main.ts (this name looks like the startup file) and notice that the code inside is very simple:

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

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

Finding main.ts does one thing: introduce AppModule and create a service that listens on port 3000 through the NestFactory factory function.

app.module.ts

Next open the app.module.ts file and see the following code:

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

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

As you can see, Module files are decorated with the @module decorator, which is one of the features of NestJS: NestJS claims to be js Spring because it borroys a lot from spring; The code in main.ts looks like SpringBoot. Here are three properties:

attribute role
imports Used to introduce additional modules
controllers Used to introduce additional controllers
providers It is used to introduce other providers. The Service introduced above is also a kind of Provider

So we know that app.module.ts is used to introduce the AppController controller and AppService service.

app.controller.ts

Next, we can go to app.Controller.ts, which reads as follows:

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

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

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

Private readOnly appService: appService. This line of code registers the corresponding service.

Once the service is registered, you can see that in getHello the service is called through this (there is no assignment, but this can be called later), and that each method of the controller is the interface for each request, such as getHello, which is declared as a Get request by @get.

app.service.ts

If you look at app.service.ts, it’s very simple:

import { Injectable } from '@nestjs/common';

@Injectable(a)export class AppService {
  getHello(): string {
    return 'Hello World! '; }}Copy the code

The only difference with this file is that it decorates classes with the @Injectable decorator. This decorator is used for dependency injection, a concept spring is well known for.

The @Injectable object can be defined (nest manages the assignment), just as app.Controller.ts did with AppService.

app.controller.spect.ts

Finally, look at the file app.controller.spect.ts, which reads as follows:

import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController'.() = > {
  let appController: AppController;

  beforeEach(async() = > {const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [AppService],
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root'.() = > {
    it('should return "Hello World!" '.() = > {
      expect(appController.getHello()).toBe('Hello World! ');
    });
  });
});
Copy the code

Find it is a unit test file, general development can be ignored, this is to ensure the quality of a code, is another content, leave it.

Start the service

Using the NPM run start command, we can quickly start a service and see the following in the browser:

This way, even if you run the Nest. js project, you’ve simply accomplished your goal.

conclusion

Today I learned the following points:

  1. npm install -g @nestjs/cliThe installationnestScaffolding;
  2. nest new project-nameCreate a newnestThe project;
  3. nest.jsIs a referencespringSo projects are flooded with decorators to build applications;
  4. Contact the decorator as follows:
    1. @module: indicates that the object is a Module object;
    2. @Controller: indicates that the object is a Controller object.
    3. @get: indicates that this function is a Controller interface method;
    4. @Injectable: Indicates that the object is dependency Injectable.

To be continued…