node.js - 关于koa2 的ctx.body是什么?
PHPz
PHPz 2017-04-17 16:01:24
0
3
1361
const Koa = require('koa');
const app = new Koa();

app.use(ctx => {
  ctx.body = 'Hello Koa in app-async.js';
  console.log(ctx)
});

app.listen(3000);

ctx是上下文的意思,但它究竟是什么呢?为了试图搞明白,用console.log将它输出,但是返回的内容里并没有ctx或content之类的东西。

{ request:
   { method: 'GET',
     url: '/',
     header:
      { host: 'localhost:3000',
        connection: 'keep-alive',
        'cache-control': 'max-age=0',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'accept-encoding': 'gzip, deflate, sdch, br',
        'accept-language': 'zh-CN,zh;q=0.8' } },
  response:
   { status: 200,
     message: 'OK',
     header:
      { 'content-type': 'text/plain; charset=utf-8',
        'content-length': '25' } },
  app: { subdomainOffset: 2, proxy: false, env: 'development' },
  originalUrl: '/',
  req: '<original node req>',
  res: '<original node res>',
  socket: '<original node socket>' 
}

ctx是什么?
ctx.body又是什么?为什么是body?我试过写成ctx.head,会报错~
如果换成ctx.response.body这样写,输出结果貌似是一样的,它们两个是等价的吗?

谢谢~

PHPz
PHPz

学习是最好的投资!

reply all(3)
Peter_Zhu

ctx is the abbreviation of context. In Chinese, it is generally called context. This noun is found in all languages. It can be understood as the environment for communication between upper (request) and lower (response). Therefore, Koa encapsulates both of them into ctx objects. The explanation in the koa official documentation is for the convenience of calling, ctx.req=ctx.request, ctx.res=ctx.response, similar to the soft connection in the Linux system? The final execution is still the request and response objects

So the second question:
body is the response body in the http protocol, and header refers to the response header
ctx.body = ctx.res.body = ctx.response.body

See the official documentation on github for more details
https://github.com/koajs/koa/...

巴扎黑

https://github.com/koajs/koa/...

ctx.body 就是 ctx.response.body 的别名而已。

Response aliases

  • ctx.bodyctx.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.lastModified=

  • ctx.etag=

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.lastModified=🎜🎜 🎜🎜ctx.etag=🎜🎜 🎜 🎜
巴扎黑

It is a web framework that focuses on the HTTP life cycle, that is, from agreeing to the request to the end of the response. CTX refers to the context of the HTTP life cycle

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template