lib/application.js
function respond () {
  let res = this.res
  let body = this.body
  let code = this.status

  if (this.respond === false) return endContext(this)
  if (res.headersSent || this._finished != null) return

  this.set('server', 'Toa/' + Toa.VERSION)
  if (this.config.poweredBy) this.set('x-powered-by', this.config.poweredBy)
  // ignore body
  if (statuses.empty[code]) {
    // strip headers
    this.body = null
    res.end()
  } else if (this.method === 'HEAD') {
    if (isJSON(body)) this.length = Buffer.byteLength(JSON.stringify(body))
    res.end()
  } else if (body == null) {
    // status body
    this.type = 'text'
    body = this.message || String(code)
    this.length = Buffer.byteLength(body)
    res.end(body)
  } else if (typeof body === 'string' || Buffer.isBuffer(body)) {
    res.end(body)
  } else if (body instanceof Stream) {
    body.pipe(res)
    // to ensure `res.headersSent === true` before `context.emit("end")`
    // if "error" occured before "data", it will goto `onResError(this, error)`
    body.once('data', () => endContext(this))
    return
  } else {
    // body: json
    body = JSON.stringify(body)
    this.length = Buffer.byteLength(body)
    res.end(body)
  }
  endContext(this)
}
function respond () {
  let res = this.res
  let body = this.body
  let code = this.status

  if (this.respond === false) return endContext(this)
  if (res.headersSent || this._finished != null) return

  this.set('server', 'Toa/' + Toa.VERSION)
  if (this.config.poweredBy) this.set('x-powered-by', this.config.poweredBy)
  // ignore body
  if (statuses.empty[code]) {
    // strip headers
    this.body = null
    res.end()
  } else if (this.method === 'HEAD') {
    if (isJSON(body)) this.length = Buffer.byteLength(JSON.stringify(body))
    res.end()
  } else if (body == null) {
    // status body
    this.type = 'text'
    body = this.message || String(code)
    this.length = Buffer.byteLength(body)
    res.end(body)
  } else if (typeof body === 'string' || Buffer.isBuffer(body)) {
    res.end(body)
  } else if (body instanceof Stream) {
    body.pipe(res)
    // to ensure `res.headersSent === true` before `context.emit("end")`
    // if "error" occured before "data", it will goto `onResError(this, error)`
    body.once('data', () => endContext(this))
    return
  } else {
    // body: json
    body = JSON.stringify(body)
    this.length = Buffer.byteLength(body)
    res.end(body)
  }
  endContext(this)
}
function respond ( ) { let res = this . res let body = this . body let code = this . status if ( this . respond === false ) return endContext ( this ) if ( res . headersSent || this . _finished != null ) return this . set ( 'server' , 'Toa/' + Toa . VERSION ) if ( this . config . poweredBy ) this . set ( 'x-powered-by' , this . config . poweredBy ) // ignore body if ( statuses . empty [ code ] ) { // strip headers this . body = null res . end ( ) } else if ( this . method === 'HEAD' ) { if ( isJSON ( body ) ) this . length = Buffer . byteLength ( JSON . stringify ( body ) ) res . end ( ) } else if ( body == null ) { // status body this . type = 'text' body = this . message || String ( code ) this . length = Buffer . byteLength ( body ) res . end ( body ) } else if ( typeof body === 'string' || Buffer . isBuffer ( body ) ) { res . end ( body ) } else if ( body instanceof Stream ) { body . pipe ( res ) // to ensure `res.headersSent === true` before `context.emit("end")` // if "error" occured before "data", it will goto `onResError(this, error)` body . once ( 'data' , ( ) => endContext ( this ) ) return } else { // body: json body = JSON . stringify ( body ) this . length = Buffer . byteLength ( body ) res . end ( body ) } endContext ( this ) }
Response middleware.
Response middleware .