| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> JavaScript知识库 -> 小满nestjs(第二章-控制器) -> 正文阅读 |
|
|
[JavaScript知识库]小满nestjs(第二章-控制器) |
1.nestJS创建子模块大家如果学过angular 命令其实很像的 nest -h (呼出帮助命令)
绿色的是全名,蓝色的是别名 创建一个controller
创建子module
|
@Request(),@Req() | req |
@Response(),@Res()* | res |
@Next() | next |
@Session() | req.session |
@Param(key?: string) | req.params/req.params[key] |
@Body(key?: string) | req.body/req.body[key] |
@Query(key?: string) | req.query/req.query[key] |
@Headers(name?: string) | req.headers/req.headers[name] |
@Ip() | req.ip |
@HostParam() | req.hosts |
get 用query,post用body
import { Body, Controller, Get, Header, HttpCode, Param, Post, Query, Req } from '@nestjs/common';
@Controller('index')
export class IndexController {
@Post()
list(@Body() body ):any {
return body
}
}
import { Body, Controller, Get, Header, HttpCode, Param, Post, Query, Req } from '@nestjs/common';
@Controller('index')
export class IndexController {
@Get()
list(@Query() query ):any {
return query
}
}
query 和 body 都可以接受参数
import { Body, Controller, Get, Header, HttpCode, Param, Post, Query, Req } from '@nestjs/common';
@Controller('index')
export class IndexController {
@Get()
list(@Query('age') query ):any {
return query
}
}
这样就直接读取age 的值并返回

?动态路由(跟vue用法一样冒号后面跟参数)param 接受参数
import { Body, Controller, Get, Header, HttpCode, Param, Post, Query, Req } from '@nestjs/common';
@Controller('index')
export class IndexController {
@Get(':id')
list(@Param('id') query): any {
return query
}
}
@HttpCode(500)
响应的状态码总是默认为?200,除了 POST 请求(默认响应状态码为?201),我们可以通过在处理函数外添加?@HttpCode(...)?装饰器来轻松更改此行为。
import { Body, Controller, Get, Header, HttpCode, Param, Post, Query, Req } from '@nestjs/common';
@Controller('index')
export class IndexController {
@Get(':id')
@HttpCode(500)
list(@Param('id') query): any {
return query
}
}

@Header('Cache-Control', 'none')?
要指定自定义响应头,可以使用?@header()?装饰器或类库特有的响应对象,(并直接调用?res.header())。
import { Body, Controller, Get, Header, HttpCode, Param, Post, Query, Req } from '@nestjs/common';
@Controller('index')
export class IndexController {
@Get(':id')
@Header('Cache-Control', 'none')
list(@Param('id') query): any {
return query
}
}

?
|
|
| JavaScript知识库 最新文章 |
| ES6的相关知识点 |
| react 函数式组件 & react其他一些总结 |
| Vue基础超详细 |
| 前端JS也可以连点成线(Vue中运用 AntVG6) |
| Vue事件处理的基本使用 |
| Vue后台项目的记录 (一) |
| 前后端分离vue跨域,devServer配置proxy代理 |
| TypeScript |
| 初识vuex |
| vue项目安装包指令收集 |
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年11日历 | -2025/11/25 4:19:06- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |