nest controller

110 阅读1分钟

使用套路

@Controller("/test")
export class HelloController{
   @Get("/test1")
   async hello():Promise<string>{
   return "test123"
   }
}

// controller 写入 到modules中 // modules 在根module中使用

@Module({
    controllers: [HelloController]
})
export class TestModule {

}


@Module({
  imports: [TestModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

访问成功了 localhost:7001/test/test1

controller 方法的一些尝试


@Request(),@Req()
@Response(),@Res()*
@Next()
@Session()
@Query(key?: string)
@Body(key?: string)
@Headers(name?: string)
@Ip()
@HostParam()

使用vscode的http-request 测试接口

VSCode的REST Client指南,超好用的HTTP客户端工具_南瓜慢说的博客-CSDN博客_vscode-restclient

@Get('testip')
testIp(@Ip() ip:string){
return ip
}
@Get('/testheader')
testIp(@Headers() headers:any){
    return headers
}
@Get('/testheaderUseragent')
testIp(@Headers('user-agent') headerUseragent:string){
    return headerUseragent
}

{ "user-agent": "vscode-restclient",
"accept-encoding": "gzip, deflate", 
"host": "localhost:7001", 
"connection": "close" }

我们看到
testIp(@Headers() headers:any)
这个 any是可以优化的
any的类型 我们来之express的headers


import {Request} from 'express'
type Headers=Request.headers

import {Request} from 'express'

使用@Req
hello(@Req() req:Request){
  var aaa=req.headers
    看看aaa的类型是什么?
    import {IncomingHttpHeaders} from 'http'
    我们知道是IncomingHttpHeaders
    
}