zoukankan      html  css  js  c++  java
  • 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() 装饰器注释的类

      

  • 相关阅读:
    MATLAB调用VISUAL STUDIO 编写的C++函数
    卡尔曼滤波
    资料(不定时更新)
    20201207-总结
    20201126-1 每周例行报告
    20201120-1 每周例行报告
    作业要求 20201112-1 每周例行报告
    20201105-1 每周例行报告
    作业要求 20201029-1 每周例行报告
    作业要求 20201022-1 每周例行报告
  • 原文地址:https://www.cnblogs.com/catherinezyr/p/14680787.html
Copyright © 2011-2022 走看看