From 874a776dbeacbc46c5461a95bb049f9996df1e14 Mon Sep 17 00:00:00 2001 From: undefined Date: Mon, 18 May 2020 11:42:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=BC=80=E5=8F=91=E6=8C=87?= =?UTF-8?q?=E5=8D=97=E4=B9=8B=E6=B3=A8=E5=86=8C=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/wiki/raw/development/contents.json | 4 +- module/wiki/raw/development/handler.md | 88 +++++++++++++++++++++++ module/wiki/raw/development/service.md | 3 + 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 module/wiki/raw/development/handler.md create mode 100644 module/wiki/raw/development/service.md diff --git a/module/wiki/raw/development/contents.json b/module/wiki/raw/development/contents.json index 649b0118b..0e96a8feb 100644 --- a/module/wiki/raw/development/contents.json +++ b/module/wiki/raw/development/contents.json @@ -1,4 +1,6 @@ [ "basic", - "module" + "module", + "handler", + "service" ] \ No newline at end of file diff --git a/module/wiki/raw/development/handler.md b/module/wiki/raw/development/handler.md new file mode 100644 index 000000000..0424ad132 --- /dev/null +++ b/module/wiki/raw/development/handler.md @@ -0,0 +1,88 @@ +# Handler + +通常用于提供页面路由。 + +例:注册新路由: + +```js +const { Handler, Route } = global.Hydro.service.server; +const { user } = global.Hydro.model; +const { PERM_LOGGEDIN } = global.Hydro.permission; + +class CustomHandler extends Handler { + async prepare() { + this.checkPassword(PERM_LOGGEDIN); + } + + async get({ username, password }) { + const udoc = await user.getByUname(username); + udoc.checkPassword(password); + this.response.body = { udoc }; + } + + async post() { + // 当提交表单时会执行该函数 + } + + async postConfirm() { + // 当提交表单并存在 operation 值为 confirm 时执行。 + } +} + +async function apply() { + Route('/route/:username', module.exports.CustomHandler); +} + +module.exports = { CustomHandler, apply }; +``` + +在路由中定义所有的函数应均为异步函数,支持的函数如下: + +_prepare, prepare, get, post, post[Operation], cleanup, _cleanup + +具体流程如下: + +先执行 _prepare(args) (如果存在) +args 为传入的参数集合(包括 QueryString, Body, Path)中的全部参数,并对以下字段进行了校验: + +| 名称 | 类型 | +| :------: | :-----------: | +| content | string | +| title | string | +| uid | number(int) | +| password | string | +| mail | string(mail) | +| uname | string | +| page | number(int) | +| duration | number(float) | +| role | string | +| roles | string[] | +| pids | string | +| tid | bson.ObjectID | +| rid | bson.ObjectID | +| did | bson.ObjectID | +| drid | bson.ObjectID | +| drrid | bson.ObjectID | +| psid | bson.ObjectID | +| psrid | bson.ObjectID | +| docId | bson.ObjectID | +| mongoId | bson.ObjectID | + +再执行 prepare(args) (如果存在) +检查请求类型: + +为 GET ? + -> 执行 get(args) +为 POST ? + -> 执行 post(args) + -> 含有 operation 字段? + -> 执行 post[Operation] + +执行 cleanup() +执行 _cleanup() + +如果在 this.response.template 指定模板则渲染,否则直接返回 this.response.body 中的内容。 + +* 在表单提交时的 operation 字段使用下划线,函数名使用驼峰命名。 + +如 `` 对应 `postConfirmDelete` 函数。 diff --git a/module/wiki/raw/development/service.md b/module/wiki/raw/development/service.md new file mode 100644 index 000000000..4774c18e1 --- /dev/null +++ b/module/wiki/raw/development/service.md @@ -0,0 +1,3 @@ +# Service + +通常用于提供与其他程序对接的接口。(如内置的 MongoDB 等)