@celljs/mvc 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.zh-cn.md +97 -0
  2. package/package.json +4 -4
@@ -0,0 +1,97 @@
1
+ # Cell - MVC 组件
2
+
3
+ Cell 后端的控制器:带 `@Controller` 的类,其方法回应 HTTP 请求。它在 `@celljs/web` 上运行(`node:http`,没有 express)。指南:[HTTP APIs](https://cell.cellbang.com/zh/guide/web)。
4
+
5
+ ```bash
6
+ npm install @celljs/mvc @celljs/express-adapter
7
+ ```
8
+
9
+ `@celljs/express-adapter` 是启动服务器的后端入口;`cell init` 模板已经依赖这两个包。
10
+
11
+ ```ts
12
+ import { Body, Controller, Get, Param, Post } from '@celljs/mvc/lib/node';
13
+
14
+ @Controller('users')
15
+ export class UserController {
16
+ @Get(':id')
17
+ get(@Param('id') id: number) {
18
+ return { id }; // JSON
19
+ }
20
+
21
+ @Post()
22
+ create(@Body() user: { name: string }) {
23
+ return user;
24
+ }
25
+ }
26
+ ```
27
+
28
+ 从 DI 模块中导入控制器的文件(`src/node/module.ts`),否则它不会被加载。
29
+
30
+ ## 路由
31
+
32
+ | 装饰器 | |
33
+ | --- | --- |
34
+ | `@Controller(path?)` | 一个组件(单例,为 AOP 代理),其方法是 `path` 下的路由。还接受 `ComponentOption`:`@Controller({ path: 'users', rebind: true })` |
35
+ | `@Get`、`@Post`、`@Put`、`@Patch`、`@Delete`、`@Head`、`@Options` | 一种 HTTP 方法;参数是控制器下的路径(`':id'`),对于控制器路径为空 |
36
+ | `@All(path?)` | 每种方法 |
37
+
38
+ 路径语法:`:name` 是一个段,`*` 匹配任何东西,`(...)` 是可选的。路由挂载在 `server.path`(默认 `/`)下;`mvc.path`(默认 `/*`)是 MVC 处理器接受的请求模式。与任何路由不匹配的请求得到 `404`,消息中包含方法和路径。
39
+
40
+ ## 参数
41
+
42
+ | 装饰器 | 值 |
43
+ | --- | --- |
44
+ | `@Param(name?)` | 一个路径参数,或所有参数 |
45
+ | `@Query(name?)` | 一个查询参数,或整个解析的查询 |
46
+ | `@Body(name?)` | 解析体的一个字段,或整个体 |
47
+ | `@Header(name?)` | 一个请求头,或所有头 |
48
+ | `@Cookie(name?)` | 一个请求 cookie |
49
+ | `@Session(name?)` | 一个会话值 |
50
+ | `@Req()` / `@Res()` | `@celljs/web` 的请求 / 响应对象 |
51
+
52
+ 类型为 `String`、`Number`、`Boolean` 或 `Date` 的参数会被转换。使用类类型的参数在安装 [`@celljs/validation`](../validation) 时会被验证。
53
+
54
+ ## 响应
55
+
56
+ | 装饰器 | 响应 |
57
+ | --- | --- |
58
+ | 无或 `@Json()` | 返回值作为 JSON(`mvc.defaultViewName: json`) |
59
+ | `@Text()` | 返回值作为 `text/plain` |
60
+ | `@Html(template?)` | 用返回值渲染的 Mustache 模板(见下文) |
61
+ | `@File(file?)` | 方法返回的 Buffer 或流,或 `mvc.baseFileDir` 中的文件 `file`(默认 `assets`,相对于服务器代码的目录) |
62
+ | `@View(name, metadata?)` | 按名称的视图;通过绑定 `View` 添加视图 |
63
+ | `@Header(name, value)` | 设置响应头 |
64
+ | `@Cookie(name, value)` | 设置响应 cookie |
65
+ | `@Session(name, value)` | 在会话中存储一个值 |
66
+
67
+ 方法可以是 `async`。返回 `undefined` 回应 `200` 和空体。在响应上设置另一个状态(`@Res() res`,`res.statusCode = 201`)并照常返回体。
68
+
69
+ `mvc.jsonView.template` 用模板包装每个 JSON 响应和错误,用于回应 `{ "success": ..., "data": ... }` 的 API:`{{model}}`(JSON,无时为 `null`)、`{{error}}`、`{{success}}` 和 `{{code}}` 被替换,每次出现;`{{error}}` 和 `{{code}}` 为 JSON 字符串转义,因此在引号内写入。
70
+
71
+ ## 错误
72
+
73
+ 抛出 `HttpError(status, message)` 或从 `@celljs/web/lib/node` 抛出一个子类如 `NotFoundError` 以该状态回应;其他错误回应 `500`。堆栈跟踪不被发送。`@celljs/web` 的错误处理器用 `ErrorMessageResolver` 允许的消息回应:在生产中服务器错误仅给出其状态文本(`web.errors.exposeMessages` 更改它)。使用 `mvc.jsonView.template`,`JsonViewErrorHandler`(优先级 2000)用模板回应每个错误并保留响应的状态。
74
+
75
+ `@Catch(ErrorType | ErrorType[])` 在控制器的方法上处理其路由抛出的那些类型的错误;该方法获得错误,其返回值是响应:
76
+
77
+ ```ts
78
+ @Catch(QuotaError)
79
+ @Text()
80
+ onQuota(err: QuotaError) {
81
+ return `handled: ${err.message}`;
82
+ }
83
+ ```
84
+
85
+ 应用程序范围的错误处理是 `@celljs/web` 的 `ErrorHandler`。
86
+
87
+ ## HTML 视图
88
+
89
+ `@Html('page.mustache')` 从 `mvc.mustache.baseViewDir`(默认 `assets/views`)用方法返回的值渲染模板。`mvc.mustache.cache: true` 在内存中保留编译的模板。
90
+
91
+ 模板使用 Mustache 语法,没有第三方引擎:
92
+
93
+ - `{{name}}` 写一个值,HTML 转义,`{{{name}}}` 或 `{{& name}}` 原样;`user.name` 读一个路径,`.` 是当前项;
94
+ - `{{#list}}...{{/list}}` 为每个项重复,进入对象,或在值是真值时显示;`{{^list}}...{{/list}}` 在它是假值或为空时显示;
95
+ - `{{! comment }}` 写不了。
96
+
97
+ 不支持 Partial(`{{> name}}`)和其他分隔符(`{{=<% %>=}}`):这样的模板在编译时失败。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/mvc",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -34,9 +34,9 @@
34
34
  "watch": "tsc -b -w --preserveWatchOutput"
35
35
  },
36
36
  "dependencies": {
37
- "@celljs/core": "4.6.0",
38
- "@celljs/http": "4.6.0",
39
- "@celljs/web": "4.6.0"
37
+ "@celljs/core": "4.7.0",
38
+ "@celljs/http": "4.7.0",
39
+ "@celljs/web": "4.7.0"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public"