Context
Extends:
koa/lib/context → Context
The app's context
Constructor Summary
Public Constructor | ||
public |
|
Member Summary
Public Members | ||
public |
body: * |
|
public get |
The `app.config` delegation
|
|
public get |
logger: winston.Logger: * The `app.logger` delegation
|
|
public |
params: * |
|
public |
|
|
public get |
Check if the request was an `XMLHttpRequest`.
|
Method Summary
Public Methods | ||
public |
getService(key: *): Mixed The `app.getService` delegation
|
|
public |
Send JSON response
|
|
public |
Send JSON response with JSONP callback support.
|
|
public |
Render `view` with the given `options`
|
|
public |
Transfer the file at the given `path`.
|
Public Constructors
public constructor source
Public Members
public body: * source
public get logger: winston.Logger: * source
The app.logger
delegation
Return:
winston.Logger |
Example:
ctx.logger.info('log somtehing')
public params: * source
Public Methods
public getService(key: *): Mixed source
The app.getService
delegation
Params:
Name | Type | Attribute | Description |
key | * |
Return:
Mixed | service |
Example:
let db = ctx.getService('sequelize')
public json(obj: String | Number | Boolean | Object): void source
Send JSON response
Return:
void |
Example:
ctx.json(null);
ctx.json({ user: 'tj' });
public jsonp(obj: String | Number | Boolean | Object, Name: String): void source
Send JSON response with JSONP callback support.
Return:
void |
Example:
ctx.jsonp(null);
ctx.jsonp({ user: 'tj' });
public * render(view: String, options: Object): void source
Render view
with the given options
Return:
void |
Example:
yield ctx.render('site', { name: 'trek' })
public * sendFile(path: String, options: Object): void source
Transfer the file at the given path
.
Automatically sets the Content-Type response header field.
When an error occurs. Be sure to check res.sentHeader
if you wish to attempt responding, as the header and some data
may have already been transferred.
Return:
void |
Example:
// The following example illustrates how `ctx.sendFile()` may
// be used as an alternative for the `static()` middleware for
// dynamic situations. The code backing `res.sendFile()` is actually
// the same code, so HTTP cache support etc is identical.
app.get('/user/:uid/photos/:file', function* (next){
var uid = req.params.uid
, file = req.params.file
var yes = yield req.user.mayViewFilesFrom(uid)
if (yes) {
yield ctx.sendFile('/uploads/' + uid + '/' + file)
} else {
ctx.status = 403
ctx.body = 'Sorry! you cant see that.'
}
});