nestjs-學習筆記

搭建demo的話,看官方文檔就可以。

1.控制器(controller)

負責處理傳入的 請求 和向客戶端返回 響應 。每個控制器可以有多個路由,不同路由負責處理不同的操作。

import { Controller, Get, Post, Query, Req, Response, Body, HttpCode, HttpStatus } from '@nestjs/common';
import { AppService } from './app.service';
import { Request } from 'express';

@Controller('api')
export class AppController {
  constructor(private readonly appService: AppService) {}
    // 常用裝飾器:https://docs.nestjs.cn/7/controllers?id=request
    @Get()
    getHello(@Query() { index, key }, @Req() req: Request, @Response() res): string {
        return this.appService.getHello(index, key);
    }
    @Get('/version') // /api/version
    getVersion(@Query() query): Object {
        return this.appService.getVersion();
    }
    // 星號被用作通配符,將匹配任何字符組合
    @Get('a*') 
    findAll() {
        return 'This route uses a wildcard';
    }
    @Post('/index') // /api/index
    @HttpCode(HttpStatus.NO_CONTENT)
    postIndex(@Body() body): Object {
        return 1;
    }

}

自定義響應頭,比如處理該路由的下請求的cors.

2.提供者(providers)

是一個用 @Injectable() 裝飾器註釋的類

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章