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

      

  • 相关阅读:
    每日日报7月15日
    每日日报7月14日
    ecplise下 java问题处理
    Visual Studio Code for .Net Framework
    Go语言操作MySQL数据库
    Go语言Gin-4中间件
    Go语言Gin-2.数据传输
    Go语言Gin-1.路由
    13.Go语言-并发编程
    12.Go语言-网络编程
  • 原文地址:https://www.cnblogs.com/catherinezyr/p/14680787.html
Copyright © 2011-2022 走看看