Skip to content
This repository has been archived by the owner on Jun 2, 2021. It is now read-only.

Latest commit

 

History

History
1035 lines (689 loc) · 27.1 KB

guide.md

File metadata and controls

1035 lines (689 loc) · 27.1 KB

Toa

简洁而强大的 web 框架。

Thanks to Koa and it's authors

Summary

Toa 简介

Toa 修改自 Koa,基本架构原理与 Koa 相似,contextrequestresponse 三大基础对象几乎一样。

Toa 是基于 thunks 组合业务逻辑,来实现异步流程控制和异常处理。thunks 是一个比 co 更强大的异步流程控制工具,支持所有形式的异步控制,包括 callback,promise,generator,async/await 等。

Toa 支持 Node.js v0.12 以上,但在高版本中将有更好的体验和性能,如 >=v4 的版本支持 generator 函数,>=v7 的版本支持 async/await 函数,这些特性支持用同步逻辑编写非阻塞的异步程序

ToaKoa 学习成本和编程体验是一致的,两者之间几乎是无缝切换。但 Toa 去掉了 Koa级联(Cascading) 逻辑,强化中间件,尽量削弱第三方组件访问应用的能力,使得编写大型应用的结构逻辑更简洁明了,也更安全。

安装 Toa

npm install toa

Application

一个 Toa Application(以下简称 app)由一系列 中间件 组成。中间件 是指通过 app.use 加载的同步函数、thunk 函数、generator 函数或 async/await 函数。

对于 web server 的一次访问请求,app 会按照顺序先运行中间件,然后再运行业务逻辑中 context.after(hook) 动态添加的 hooks,最后运行内置的 respond 函数,将请求结果自动响应的客户端。由于 Toa 没有 级联(Cascading),这些中间件或模块的运行不会有任何交叉,它们总是先运行完一个,再运行下一个。

Toa 只有一个极简的内核,提供快捷的 HTTP 操作和异步流程控制能力。具体的业务功能逻辑则由中间件和模块组合实现。用户则可根据自己的业务需求,以最轻量级的方式组合自己的应用。

让我们来看看 Toa 极其简单的 Hello World 应用程序:

const Toa = require('toa')
const app = new Toa()

app.use(function () {
  this.body = 'Hello World!\n-- toa'
})

app.listen(3000)

Class: Toa()

Class: Toa(server)

Class: Toa(options)

Class: Toa(onerror)

Class: Toa(server, options)

  • server: {Object}, http server 或 https server 实例。
  • options: {Object} 类似 thunks 的 options,对于 server 的每一个 client request,toa app 均会用 thunks 生成一个的 thunk,挂载到 context.thunk,该 thunk 的作用域对该 client request 的整个生命周期生效。
    • options.onerror: {Function} 其 thisclient requestcontext 对象。当 client request 处理流程出现异常时,会抛出到 onerror,原有处理流程会终止,onerror 运行完毕后再进入 toa 内置的异常处理流程,最后 respond 客户端。如果 onerror 返回 true,则会忽略该异常,异常不会进入内置异常处理流程,然后直接 respond 客户端。
// with full arguments
const app = new Toa(server, {
  onerror: function (error) {}
})

app.keys = ['key1', 'key2']

设置 cookie 签名密钥,参考 Keygrip。 注意,签名密钥只在配置项 signed 参数为真时才会生效:

this.cookies.set('name', 'test', {signed: true})

app.config = config

config 会被 context.config 继承,但 context.config 不会修改 app.config

app.config = config

app.config 默认值:

{
  proxy: false, // 决定了哪些 `proxy header` 参数会被加到信任列表中
  env: process.env.NODE_ENV || 'development', // node 执行环境
  subdomainOffset: 2,
  poweredBy: 'Toa',
  secureCookie: null
}

app.use(function () {})

app.use(function (callback) {})

app.use(function * () {})

app.use(async function () {})

加载中间件,返回 appfn 必须是 thunk 函数或 generator 函数,函数中的 this 值为 context

app.use(function (callback) {
  // task
  // this === context
  callback(err, result)
})
app.use(function * () {
  // task
  // this === context
  yield result
})

app.onerror = function (error) {}

设置 onerror 函数,当 app 捕捉到程序运行期间的错误时,会先使用 options.onerror(若提供)处理,再使用内置的 onResError 函数处理响应给客户端,最后抛出给 app.onerror 处理,应用通常可以在这里判断错误类型,根据情况将错误写入日志系统。

// default
app.onerror = function (err) {
  // ignore null and response error
  if (err == null || (err.status && err.status < 500)) return
  if (!util.isError(err)) err = new Error('non-error thrown: ' + err)

  // catch system error
  let msg = err.stack || err.toString()
  console.error(msg.replace(/^/gm, '  '))
}

app.toListener()

返回 app request listener。

const http = require('http')
const toa = require('toa')

const app = toa()
const server = http.createServer(app.toListener())
server.listen(3000)

等效于:

const toa = require('toa')

const app = toa()
app.listen(3000)

app.listen(port, [hostname], [backlog], [callback])

app.listen(path, [callback])

app.listen(handle, [callback])

返回 server,用法与 httpServer.listen 一致。

// 与 httpServer.listen 一致
app.listen(3000)

Context

Similar to Koa's Context

Difference from Koa

  • remove ctx.app
  • add ctx.thunk method, it is thunk function that bound a scope with onerror.
  • add ctx.end method, use to stopping request process and respond immediately.
  • add ctx.after method, use to add hooks that run after middlewares and before respond.
  • add ctx.catchStream method, used to catch stream's error or clean stream when some error.
  • add ctx.ended property, indicates that the response ended.
  • add ctx.finished property, indicates that the response finished successfully.
  • add ctx.closed property, indicates that the response closed unexpectedly.
  • context is a EventEmitter instance

Context object encapsulates node's request and response objects into a single object which provides many helpful methods for writing web applications and APIs. These operations are used so frequently in HTTP server development that they are added at this level instead of a higher level framework, which would force middleware to re-implement this common functionality.

A Context is created per request, and is referenced in middleware as the receiver, or the this identifier, as shown in the following snippet:

const app = Toa(function * () {
  this // is the Context
  this.request // is a toa Request
  this.response // is a toa Response
})

app.use(function * () {
  this // is the Context
  this.request // is a toa Request
  this.response // is a toa Response
})

Many of the context's accessors and methods simply delegate to their ctx.request or ctx.response equivalents for convenience, and are otherwise identical. For example ctx.type and ctx.length delegate to the response object, and ctx.path and ctx.method delegate to the request.

Events

'close'

Emitted after a HTTP request closed, indicates that the socket has been closed, and context.closed will be true.

'end'

Emitted after respond() was called, indicates that body was sent. and context.ended will be true

'finish'

Emitted after a HTTP response finished. and context.finished will be true.

'error'

A context always listen 'error' event by ctx.onerror. ctx.onerror is a immutable error handle. So you can use ctx.emit('error', error) to deal with your exception or error.

API

Context specific methods and accessors.

ctx.thunk([thunkable])

A thunk function that bound a scope.

ctx.end([message])

Use to stopping request process and respond immediately. It should not run in try catch block, otherwise onstop will not be trigger.

ctx.after(function () {})

ctx.after(function (callback) {})

ctx.after(function * () {})

ctx.after(async function () {})

Add hooks dynamicly. Hooks will be executed in LIFO order after middlewares, but before respond.

ctx.req

Node's request object.

ctx.res

Node's response object.

Bypassing Toa's response handling is not supported. Avoid using the following node properties:

  • res.statusCode
  • res.writeHead()
  • res.write()
  • res.end()

ctx.request

A Toa Request object.

ctx.response

A Toa Response object.

ctx.state

The recommended namespace for passing information through middleware and to your frontend views.

this.state.user = yield User.find(id)

ctx.cookies.get(name, [options])

Get cookie name with options:

  • signed the cookie requested should be signed

Toa uses the cookies module where options are simply passed.

ctx.cookies.set(name, value, [options])

Set cookie name to value with options:

  • signed sign the cookie value
  • expires a Date for cookie expiration
  • path cookie path, /' by default
  • domain cookie domain
  • secure secure cookie
  • httpOnly server-accessible cookie, true by default

Toa uses the cookies module where options are simply passed.

ctx.throw([msg], [status], [properties])

Helper method to throw an error with a .status property defaulting to 500 that will allow Toa to respond appropriately. The following combinations are allowed:

this.throw(403)
this.throw('name required', 400)
this.throw(400, 'name required')
this.throw('something exploded')

For example this.throw('name required', 400) is equivalent to:

let err = new Error('name required')
err.status = 400
throw err

Note that these are user-level errors and are flagged with err.expose meaning the messages are appropriate for client responses, which is typically not the case for error messages since you do not want to leak failure details.

You may optionally pass a properties object which is merged into the error as-is, useful for decorating machine-friendly errors which are reported to the requester upstream.

this.throw(401, 'access_denied', {user: user})
this.throw('access_denied', {user: user})

Toa uses http-errors to create errors.

ctx.createError([status], [msg], [properties])

Similar to ctx.throw, create a error object, but don't throw.

ctx.assert(value, [status], [msg], [properties])

Helper method to throw an error similar to .throw() when !value. Similar to node's assert() method.

this.assert(this.state.user, 401, 'User not found. Please login!')

Toa uses http-assert for assertions.

ctx.respond

To bypass Toa's built-in response handling, you may explicitly set this.respond = false. Use this if you want to write to the raw res object instead of letting Toa handle the response for you.

Note that using this is not supported by Toa. This may break intended functionality of Toa middleware and Toa itself. Using this property is considered a hack and is only a convenience to those wishing to use traditional fn(req, res) functions and middleware within Toa.

ctx.catchStream(stream)

Catch a stream's error, if 'error' event emit from the stream, the error will be throw to Thunk's onerror and response it.

Request aliases

The following accessors and alias Request equivalents:

  • ctx.header
  • ctx.headers
  • ctx.method
  • ctx.method=
  • ctx.url
  • ctx.url=
  • ctx.origin
  • ctx.originalUrl
  • ctx.href
  • ctx.path
  • ctx.path=
  • ctx.query
  • ctx.query=
  • ctx.querystring
  • ctx.querystring=
  • ctx.host
  • ctx.hostname
  • ctx.fresh
  • ctx.stale
  • ctx.socket
  • ctx.protocol
  • ctx.secure
  • ctx.ip
  • ctx.ips
  • ctx.idempotent
  • ctx.subdomains
  • ctx.is()
  • ctx.accepts()
  • ctx.acceptsEncodings()
  • ctx.acceptsCharsets()
  • ctx.acceptsLanguages()
  • ctx.get()
  • ctx.search()

Response aliases

The following accessors and alias Response equivalents:

  • ctx.body
  • ctx.body=
  • ctx.status
  • ctx.status=
  • ctx.message
  • ctx.message=
  • ctx.length=
  • ctx.length
  • ctx.type=
  • ctx.type
  • ctx.headerSent
  • ctx.redirect()
  • ctx.attachment()
  • ctx.set()
  • ctx.append()
  • ctx.remove()
  • ctx.vary()
  • ctx.lastModified=
  • ctx.etag=

Request

The same as Koa's Request

Request object is an abstraction on top of node's vanilla request object, providing additional functionality that is useful for every day HTTP server development.

API

request.header

Request header object.

request.headers

Request header object. Alias as request.header.

request.method

Request method.

request.method=

Set request method, useful for implementing middleware such as methodOverride().

request.length

Return request Content-Length as a number when present, or undefined.

request.url

Get request URL.

request.url=

Set request URL, useful for url rewrites.

request.origin

Get origin of URL.

request.originalUrl

Get request original URL.

request.href

Get full request URL, include protocol, host and url.

this.request.href
// => http://example.com/foo/bar?q=1

request.path

Get request pathname.

request.path=

Set request pathname and retain query-string when present.

request.querystring

Get raw query string void of ?.

request.querystring=

Set raw query string.

request.search

Get raw query string with the ?.

request.search=

Set raw query string.

request.host

Get host (hostname:port) when present. Supports X-Forwarded-Host when app.proxy is true, otherwise Host is used.

request.hostname

Get hostname when present. Supports X-Forwarded-Host when app.proxy is true, otherwise Host is used.

request.type

Get request Content-Type void of parameters such as "charset".

let ct = this.request.type
// => "image/png"

request.charset

Get request charset when present, or undefined:

this.request.charset
// => "utf-8"

request.query

Get parsed query-string, returning an empty object when no query-string is present. Note that this getter does not support nested parsing.

For example "color=blue&size=small":

{
  color: 'blue',
  size: 'small'
}

request.query=

Set query-string to the given object. Note that this setter does not support nested objects.

this.query = {next: '/login'}

request.fresh

Check if a request cache is "fresh", aka the contents have not changed. This method is for cache negotiation between If-None-Match / ETag, and If-Modified-Since and Last-Modified. It should be referenced after setting one or more of these response headers.

this.status = 200
this.set('ETag', '123')

// cache is ok
if (this.fresh) {
  this.status = 304
  return
}

// cache is stale
// fetch new data
this.body = yield db.find('something')

request.stale

Inverse of request.fresh.

request.protocol

Return request protocol, "https" or "http". Supports X-Forwarded-Proto when app.proxy is true.

request.secure

Shorthand for this.protocol == "https" to check if a request was issued via TLS.

request.ip

Request remote address. Supports X-Forwarded-For when app.proxy is true.

request.ips

When X-Forwarded-For is present and app.proxy is enabled an array of these ips is returned, ordered from upstream -> downstream. When disabled an empty array is returned.

request.subdomains

Return subdomains as an array.

Subdomains are the dot-separated parts of the host before the main domain of the app. By default, the domain of the app is assumed to be the last two parts of the host. This can be changed by setting app.subdomainOffset.

For example, if the domain is "tobi.ferrets.example.com":

If app.subdomainOffset is not set, this.subdomains is ["ferrets", "tobi"]. If app.subdomainOffset is 3, this.subdomains is ["tobi"].

request.is(types...)

Check if the incoming request contains the "Content-Type" header field, and it contains any of the give mime types. If there is no request body, null is returned. If there is no content type, or the match fails false is returned. Otherwise, it returns the matching content-type.

// With Content-Type: text/html; charset=utf-8
this.is('html') // => 'html'
this.is('text/html') // => 'text/html'
this.is('text/*', 'text/html') // => 'text/html'

// When Content-Type is application/json
this.is('json', 'urlencoded') // => 'json'
this.is('application/json') // => 'application/json'
this.is('html', 'application/*') // => 'application/json'

this.is('html') // => false

For example if you want to ensure that only images are sent to a given route:

if (this.is('image/*')) {
  // process
} else {
  this.throw(415, 'images only!')
}

Content Negotiation

Request object includes helpful content negotiation utilities powered by accepts and negotiator. These utilities are:

  • request.accepts(types)
  • request.acceptsEncodings(types)
  • request.acceptsCharsets(charsets)
  • request.acceptsLanguages(langs)

If no types are supplied, all acceptable types are returned.

If multiple types are supplied, the best match will be returned. If no matches are found, a false is returned, and you should send a 406 "Not Acceptable" response to the client.

In the case of missing accept headers where any type is acceptable, the first type will be returned. Thus, the order of types you supply is important.

request.accepts(types)

Check if the given type(s) is acceptable, returning the best match when true, otherwise false. The type value may be one or more mime type string such as "application/json", the extension name such as "json", or an array ["json", "html", "text/plain"].

// Accept: text/html
this.accepts('html')
// => "html"

// Accept: text/*, application/json
this.accepts('html')
// => "html"
this.accepts('text/html')
// => "text/html"
this.accepts('json', 'text')
// => "json"
this.accepts('application/json')
// => "application/json"

// Accept: text/*, application/json
this.accepts('image/png')
this.accepts('png')
// => false

// Accept: text/*;q=.5, application/json
this.accepts(['html', 'json'])
this.accepts('html', 'json')
// => "json"

// No Accept header
this.accepts('html', 'json')
// => "html"
this.accepts('json', 'html')
// => "json"

You may call this.accepts() as many times as you like, or use a switch:

switch (this.accepts('json', 'html', 'text')) {
  case 'json': break
  case 'html': break
  case 'text': break
  default: this.throw(406, 'json, html, or text only')
}

request.acceptsEncodings(encodings)

Check if encodings are acceptable, returning the best match when true, otherwise false. Note that you should include identity as one of the encodings!

// Accept-Encoding: gzip
this.acceptsEncodings('gzip', 'deflate', 'identity')
// => "gzip"

this.acceptsEncodings(['gzip', 'deflate', 'identity'])
// => "gzip"

When no arguments are given all accepted encodings are returned as an array:

// Accept-Encoding: gzip, deflate
this.acceptsEncodings()
// => ["gzip", "deflate", "identity"]

Note that the identity encoding (which means no encoding) could be unacceptable if the client explicitly sends identity;q=0. Although this is an edge case, you should still handle the case where this method returns false.

request.acceptsCharsets(charsets)

Check if charsets are acceptable, returning the best match when true, otherwise false.

// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
this.acceptsCharsets('utf-8', 'utf-7')
// => "utf-8"

this.acceptsCharsets(['utf-7', 'utf-8'])
// => "utf-8"

When no arguments are given all accepted charsets are returned as an array:

// Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5
this.acceptsCharsets()
// => ["utf-8", "utf-7", "iso-8859-1"]

request.acceptsLanguages(langs)

Check if langs are acceptable, returning the best match when true, otherwise false.

// Accept-Language: en;q=0.8, es, pt
this.acceptsLanguages('es', 'en')
// => "es"

this.acceptsLanguages(['en', 'es'])
// => "es"

When no arguments are given all accepted languages are returned as an array:

// Accept-Language: en;q=0.8, es, pt
this.acceptsLanguages()
// => ["es", "pt", "en"]

request.idempotent

Check if the request is idempotent.

request.socket

Return the request socket.

request.get(field)

Return request header.


Response

The same as Koa's Response

Response object is an abstraction on top of node's vanilla response object, providing additional functionality that is useful for every day HTTP server development.

API

response.header

Response header object.

response.headers

Response header object. Alias as response.header.

response.socket

Request socket.

response.status

Get response status. By default, response.status is not set unlike node's res.statusCode which defaults to 200.

response.status=

Set response status via numeric code:

  • 100 "Continue"
  • 101 "Switching protocols"
  • 102 "Processing"
  • 200 "Ok"
  • 201 "Created"
  • 202 "Accepted"
  • 203 "Non-authoritative information"
  • 204 "No content"
  • 205 "Reset content"
  • 206 "Partial content"
  • 207 "Multi-status"
  • 300 "Multiple choices"
  • 301 "Moved permanently"
  • 302 "Moved temporarily"
  • 303 "See other"
  • 304 "Not modified"
  • 305 "Use proxy"
  • 307 "Temporary redirect"
  • 400 "Bad request"
  • 401 "Unauthorized"
  • 402 "Payment required"
  • 403 "Forbidden"
  • 404 "Not found"
  • 405 "Method not allowed"
  • 406 "Not acceptable"
  • 407 "Proxy authentication required"
  • 408 "Request time-out"
  • 409 "Conflict"
  • 410 "Gone"
  • 411 "Length required"
  • 412 "Precondition failed"
  • 413 "Request entity too large"
  • 414 "Request-uri too large"
  • 415 "Unsupported media type"
  • 416 "Requested range not satisfiable"
  • 417 "Expectation failed"
  • 418 "I'm a teapot"
  • 422 "Unprocessable entity"
  • 423 "Locked"
  • 424 "Failed dependency"
  • 425 "Unordered collection"
  • 426 "Upgrade required"
  • 428 "Precondition required"
  • 429 "Roo many requests"
  • 431 "Request header fields too large"
  • 500 "Internal server error"
  • 501 "Not implemented"
  • 502 "Bad gateway"
  • 503 "Service unavailable"
  • 504 "Gateway time-out"
  • 505 "HTTP version not supported"
  • 506 "Variant also negotiates"
  • 507 "Insufficient storage"
  • 509 "Bandwidth limit exceeded"
  • 510 "Not extended"
  • 511 "Network authentication required"

NOTE: don't worry too much about memorizing these strings, if you have a typo an error will be thrown, displaying this list so you can make a correction.

response.message

Get response status message. By default, response.message is associated with response.status.

response.message=

Set response status message to the given value.

response.length=

Set response Content-Length to the given value.

response.length

Return response Content-Length as a number when present, or deduce from this.body when possible, or undefined.

response.body

Get response body.

response.body=

Set response body to one of the following:

  • string written
  • Buffer written
  • Stream piped
  • Object json-stringified
  • null no content response

If response.status has not been set, Toa will automatically set the status to 200 or 204.

String

The Content-Type is defaulted to text/html or text/plain, both with a default charset of utf-8. The Content-Length field is also set.

Buffer

The Content-Type is defaulted to application/octet-stream, and Content-Length is also set.

Stream

The Content-Type is defaulted to application/octet-stream.

Object

The Content-Type is defaulted to application/json.

response.get(field)

Get a response header field value with case-insensitive field.

let etag = this.response.get('ETag')

response.set(field, value)

Set response header field to value:

this.set('Cache-Control', 'no-cache')

response.append(field, value)

Append additional header field with value val.

this.append('Link', '<http://127.0.0.1/>')

response.set(fields)

Set several response header fields with an object:

this.set({
  'ETag': '1234',
  'Last-Modified': date
})

response.remove(field)

Remove header field.

response.type

Get response Content-Type void of parameters such as "charset".

let ct = this.type
// => "image/png"

response.type=

Set response Content-Type via mime string or file extension.

this.type = 'text/plain; charset=utf-8'
this.type = 'image/png'
this.type = '.png'
this.type = 'png'

Note: when appropriate a charset is selected for you, for example response.type = 'html' will default to "utf-8", however when explicitly defined in full as response.type = 'text/html' no charset is assigned.

response.is(types...)

Very similar to this.request.is(). Check whether the response type is one of the supplied types. This is particularly useful for creating middleware that manipulate responses.

response.redirect(url, [alt])

Perform a [302] redirect to url.

The string "back" is special-cased to provide Referrer support, when Referrer is not present alt or "/" is used.

this.redirect('back')
this.redirect('back', '/index.html')
this.redirect('/login')
this.redirect('http://google.com')

To alter the default status of 302, simply assign the status before or after this call. To alter the body, assign it after this call:

this.status = 301
this.redirect('/cart')
this.body = 'Redirecting to shopping cart'

response.attachment([filename])

Set Content-Disposition to "attachment" to signal the client to prompt for download. Optionally specify the filename of the download.

response.headerSent

Check if a response header has already been sent. Useful for seeing if the client may be notified on error.

response.lastModified

Return the Last-Modified header as a Date, if it exists.

response.lastModified=

Set the Last-Modified header as an appropriate UTC string. You can either set it as a Date or date string.

this.response.lastModified = new Date()

response.etag=

Set the ETag of a response including the wrapped "s. Note that there is no corresponding response.etag getter.

this.response.etag = crypto.createHash('md5').update(this.body).digest('hex')

response.vary(field)

Vary on field.