Home Reference Source Test Repository
import Context from 'trek/src/Context.js'
public class | source

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

xhr: Boolean: *

Check if the request was an `XMLHttpRequest`.

Method Summary

Public Methods
public

getService(key: *): Mixed

The `app.getService` delegation
public

json(obj: String | Number | Boolean | Object): void

Send JSON response
public

jsonp(obj: String | Number | Boolean | Object, Name: String): void

Send JSON response with JSONP callback support.
public

* render(view: String, options: Object): void

Render `view` with the given `options`
public

* sendFile(path: String, options: Object): void

Transfer the file at the given `path`.

Public Constructors

public constructor source

Public Members

public body: * source

public get config: Config: * source

The app.config delegation

Return:

Config

Example:


 ctx.config
 // => app.config

public get logger: winston.Logger: * source

The app.logger delegation

Return:

winston.Logger

Example:


 ctx.logger.info('log somtehing')

public params: * source

public type: string source

public get xhr: Boolean: * source

Check if the request was an XMLHttpRequest.

Return:

Boolean

Example:


 if (ctx.xhr) ctx.body = 'AJAX'

Public Methods

public getService(key: *): Mixed source

The app.getService delegation

Params:

NameTypeAttributeDescription
key *

Return:

Mixed

service

Example:


 let db = ctx.getService('sequelize')

public json(obj: String | Number | Boolean | Object): void source

Send JSON response

Params:

NameTypeAttributeDescription
obj String | Number | Boolean | Object

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.

Params:

NameTypeAttributeDescription
obj String | Number | Boolean | Object
Name String
  • optional
  • default: 'callback'

Return:

void

Example:


    ctx.jsonp(null);
    ctx.jsonp({ user: 'tj' });

public * render(view: String, options: Object): void source

Render view with the given options

Params:

NameTypeAttributeDescription
view String

The name of view

options Object
options.cache Boolean

Boolean hinting to the engine it should cache

options.filename String

Filename of the view being rendered

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.

Params:

NameTypeAttributeDescription
path String

The file path

options Object
options.maxAge Number

Defaulting to 0 (can be string converted by ms)

options.root String

Directory for relative filenames

options.headers Object

Object of headers to serve with file

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.'
   }
 });