搭错车的小火柴

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

搭建demo的话,看官方文档就可以。

 

在vscode-code调试快捷键: cmd+shift+P,输入attach ,然后选第一个 “ Debug: attach to Node Process”,

 

 就会看到调试界面了。

 

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() 装饰器注释的类

  

posted on 2021-04-20 14:04  搭错车的小火柴  阅读(114)  评论(0编辑  收藏  举报