Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: pr#1533 pr#1566 pr#1574 pr#1577 pr#1589 #97

Merged
merged 5 commits into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions docs/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const fastify = require('fastify')({

// 同时注册 "/foo" 与 "/foo/"
fastify.get('/foo/', function (req, reply) {
res.send('foo')
reply.send('foo')
})

// 同时注册 "/bar" 与 "/bar/"
fastify.get('/bar', function (req, reply) {
res.send('bar')
reply.send('bar')
})
```

Expand Down Expand Up @@ -94,6 +94,26 @@ Fastify 依托 [Pino](https://getpino.io/) 内建了一个日志工具。该属
})
```
用户提供的序列化函数将会覆盖对应属性默认的序列化函数。
+ `loggerInstance`:自定义日志工具实例。日志工具必须实现 Pino 的接口,即拥有如下方法:`info`, `error`, `debug`, `fatal`, `warn`, `trace`, `child`。例如:
```js
const pino = require('pino')();

const customLogger = {
info: function (o, ...n) {},
warn: function (o, ...n) {},
error: function (o, ...n) {},
fatal: function (o, ...n) {},
trace: function (o, ...n) {},
debug: function (o, ...n) {},
child: function() {
const child = Object.create(this);
child.pino = pino.child(...arguments);
return child;
},
};

const fastify = require('fastify')({logger: customLogger});
```

<a name="custom-http-server"></a>
### `serverFactory`
Expand Down Expand Up @@ -142,6 +162,13 @@ fastify.get('/user/:username', (request, reply) => {

+ 默认值:`'request-id'`

<a name="factory-request-id-log-label"></a>
### `requestIdLogLabel`

定义日志中请求 id 的标签。

+ 默认值:`'reqId'`

<a name="factory-gen-request-id"></a>
### `genReqId`
用于生成请求 id 的函数。参数为来访的请求对象。
Expand All @@ -151,8 +178,11 @@ fastify.get('/user/:username', (request, reply) => {
```js
let i = 0
const fastify = require('fastify')({
genReqId: function (req) { return i++ }
genReqId: function (req) { return req.headers['request-id'] || i++ }
})
```

**注意:当设置了 'request-id' header时,genReqId _不会_ 被调用。**

<a name="factory-trust-proxy"></a>
### `trustProxy`
Expand Down Expand Up @@ -454,6 +484,10 @@ fastify.register(function (instance, opts, next) {
#### setSchemaCompiler
为所有的路由设置 schema 编译器 (schema compiler),请看[这里](https://github.com/fastify/docs-chinese/blob/master/docs/Validation-and-Serialization.md#schema-compiler)了解更多信息。

<a name="schema-compiler"></a>
#### schemaCompiler
`setSchemaCompiler` 方法的简写。用于设置 schema 编译器函数,也可用于返回全部路由的 schema 编译器。

<a name="set-not-found-handler"></a>
#### setNotFoundHandler

Expand Down
4 changes: 4 additions & 0 deletions docs/Validation-and-Serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ const ajv = new Ajv({
fastify.setSchemaCompiler(function (schema) {
return ajv.compile(schema)
})

// -------
// 此外,你还可以通过 setter 方法来设置 schema 编译器:
fastify.schemaCompiler = function (schema) { return ajv.compile(schema) })
```

你可能还想使用其他的验证库,例如 `Joi`。这时,你只要参照下面的示例,便能轻松地使用它们来校验 url 参数、请求主体与查询字符串了!
Expand Down