js return 后 运行 另_新手入门Nest.js(六) 控制器Resources、路由通配符

  • Post author:
  • Post category:其他


点击上方公众号,可快速关注

作者:鲲鹏友人
原文链接:https://www.gowhich.com/blog/1067

Nest.js控制中的Resources

前面介绍了路由中如何通过GET方式访问路由,Nest.js还支持Post、Delete、Patch、Options、Head和All等

下面看下如何使用Post

import { Controller, Get, Post, Req, Request } from '@nestjs/common';@Controller('cats')export class CatsController {  @Get()  findAll(@Req() request: Request): string {    return 'This action will returns all cats';  }  @Post()  create(): string {    return 'This action will create a new cat';  }}

项目运行起来后,做个简单的测试

$ curl -d '' http://127.0.0.1:3000/catsThis action will create a new cat

可以看到,post请求的时候访问到了create()方法

Route通配符

看个简单的例子

@Get('x*z')find() {  return 'This action uses a wildcard';}

然后启动项目做下面几个测试

$ curl http://127.0.0.1:3000/cats/xyzThis action uses a wildcard
$ curl http://127.0.0.1:3000/cats/xyyzThis action uses a wildcard
$ curl http://127.0.0.1:3000/cats/xyyzzThis action uses a wildcard

从上面的输出可以观察到,输出的内容都是一致的,说明访问的路由其实调用了同一个方法


'x*z'

路由将会匹配

xyz

,

xyyz

,

xyyzz

等,

?

,

+

,

*

,

()

都可以被用在路由的路径中

如果本文对你有帮助?请分享给更多人

f38a0924f84cde08259774abb912357a.png



版权声明:本文为weixin_39602569原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。