Similar to Koa's Context
- remove
ctx.app - add
ctx.thunkmethod, it is thunk function that bound a scope withonerror. - add
ctx.endmethod, use to stopping request process and respond immediately. - add
ctx.aftermethod, use to add hooks that run after middlewares and before respond. - add
ctx.catchStreammethod, used to catch stream's error or clean stream when some error. - add
ctx.endedproperty, indicates that the response ended. - add
ctx.finishedproperty, indicates that the response finished successfully. - add
ctx.closedproperty, indicates that the response closed unexpectedly. - context is a
EventEmitterinstance
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.
Emitted after a HTTP request closed, indicates that the socket has been closed, and context.closed will be true.
Emitted after respond() was called, indicates that body was sent. and context.ended will be true
Emitted after a HTTP response finished. and context.finished will be true.
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.
Context specific methods and accessors.
A thunk function that bound a scope.
thunkablethunkable value, see: https://github.com/thunks/thunks
Use to stopping request process and respond immediately. It should not run in try catch block, otherwise onstop will not be trigger.
messageString, see: https://github.com/thunks/thunks
Add hooks dynamicly. Hooks will be executed in LIFO order after middlewares, but before respond.
Node's request object.
Node's response object.
Bypassing Toa's response handling is not supported. Avoid using the following node properties:
res.statusCoderes.writeHead()res.write()res.end()
A Toa Request object.
A Toa Response object.
The recommended namespace for passing information through middleware and to your frontend views.
this.state.user = yield User.find(id)Get cookie name with options:
signedthe cookie requested should be signed
Toa uses the cookies module where options are simply passed.
Set cookie name to value with options:
signedsign the cookie valueexpiresaDatefor cookie expirationpathcookie path,/'by defaultdomaincookie domainsecuresecure cookiehttpOnlyserver-accessible cookie, true by default
Toa uses the cookies module where options are simply passed.
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 errNote 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.
Similar to ctx.throw, create a error object, but don't throw.
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.
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.
Catch a stream's error, if 'error' event emit from the stream, the error will be throw to Thunk's onerror and response it.
The following accessors and alias Request equivalents:
ctx.headerctx.headersctx.methodctx.method=ctx.urlctx.url=ctx.originctx.originalUrlctx.hrefctx.pathctx.path=ctx.queryctx.query=ctx.querystringctx.querystring=ctx.hostctx.hostnamectx.freshctx.stalectx.socketctx.protocolctx.securectx.ipctx.ipsctx.idempotentctx.subdomainsctx.is()ctx.accepts()ctx.acceptsEncodings()ctx.acceptsCharsets()ctx.acceptsLanguages()ctx.get()ctx.search()
The following accessors and alias Response equivalents:
ctx.bodyctx.body=ctx.statusctx.status=ctx.messagectx.message=ctx.length=ctx.lengthctx.type=ctx.typectx.headerSentctx.redirect()ctx.attachment()ctx.set()ctx.append()ctx.remove()ctx.vary()ctx.lastModified=ctx.etag=