ctx 默认的对象

  1. ctx.request,请求
  2. ctx.response,响应
  3. ctx.app,Application实例
  4. ctx.service, Server
  5. ctx.render, 渲染模板引擎
  6. ctx.cookie
  7. ctx.session
  8. ctx.curl, httpClient请求

常用的 ctx.key

  1. const ctx = [
  2. "request",
  3. "response",
  4. "app",
  5. "req",
  6. "res",
  7. "onerror",
  8. "originalUrl",
  9. "starttime",
  10. "matched",
  11. "captures",
  12. "params",
  13. "routerName",
  14. "_matchedRouteName",
  15. "routerPath",
  16. "_matchedRoute"
  17. ]
  1. {
  2. request: {
  3. method: 'GET',
  4. url: '/users/list?id=23&user=lucy',
  5. header: {
  6. 'user-agent': 'PostmanRuntime/7.28.4',
  7. accept: '*/*',
  8. 'postman-token': '01ced84c-c56f-4df5-a813-20f67f8732cc',
  9. host: '127.0.0.1:6001',
  10. 'accept-encoding': 'gzip, deflate, br',
  11. connection: 'keep-alive',
  12. cookie: 'csrfToken=SGl9XQed64inFmoCp-QjQnlL'
  13. }
  14. },
  15. response: {
  16. status: 404,
  17. message: 'Not Found',
  18. header: [Object: null prototype] { vary: 'Origin' }
  19. },
  20. app: {
  21. env: 'local',
  22. name: 'egg-bff',
  23. baseDir: '/Users/lulongwen/Documents/Project/icestark/egg-bff',
  24. subdomainOffset: 2,
  25. config: '<egg config>',
  26. controller: '<egg controller>',
  27. httpclient: '<egg httpclient>',
  28. loggers: '<egg loggers>',
  29. middlewares: '<egg middlewares>',
  30. router: '<egg router>',
  31. serviceClasses: '<egg serviceClasses>'
  32. },
  33. originalUrl: '/users/list?id=23&user=lucy',
  34. req: '<original node req>',
  35. res: '<original node res>',
  36. socket: '<original node socket>'
  37. }

全部的 ctx

  1. [
  2. "request",
  3. "response",
  4. "app",
  5. "req",
  6. "res",
  7. "onerror",
  8. "originalUrl",
  9. "starttime",
  10. "matched",
  11. "captures",
  12. "params",
  13. "routerName",
  14. "_matchedRouteName",
  15. "routerPath",
  16. "_matchedRoute",
  17. "securityOptions",
  18. "isSafeDomain",
  19. "nonce",
  20. "csrf",
  21. "ensureCsrfSecret",
  22. "rotateCsrfSecret",
  23. "assertCsrf",
  24. "safeCurl",
  25. "acceptJSONP",
  26. "createJsonpBody",
  27. "locale",
  28. "cleanupRequestFiles",
  29. "saveRequestFiles",
  30. "multipart",
  31. "getFileStream",
  32. "render",
  33. "renderView",
  34. "renderString",
  35. "view",
  36. "validate",
  37. "info",
  38. "cookies",
  39. "httpclient",
  40. "curl",
  41. "router",
  42. "helper",
  43. "getLogger",
  44. "logger",
  45. "coreLogger",
  46. "locals",
  47. "state",
  48. "runInBackground",
  49. "_runInBackground",
  50. "acceptJSON",
  51. "queries",
  52. "accept",
  53. "ip",
  54. "realStatus",
  55. "user",
  56. "unsafeRedirect",
  57. "__",
  58. "__getLocale",
  59. "__getLocaleOrigin",
  60. "__setLocale",
  61. "gettext",
  62. "inspect",
  63. "toJSON",
  64. "assert",
  65. "throw",
  66. "attachment",
  67. "redirect",
  68. "remove",
  69. "vary",
  70. "has",
  71. "set",
  72. "append",
  73. "flushHeaders",
  74. "status",
  75. "message",
  76. "body",
  77. "length",
  78. "type",
  79. "lastModified",
  80. "etag",
  81. "headerSent",
  82. "writable",
  83. "acceptsLanguages",
  84. "acceptsEncodings",
  85. "acceptsCharsets",
  86. "accepts",
  87. "get",
  88. "is",
  89. "querystring",
  90. "idempotent",
  91. "socket",
  92. "search",
  93. "method",
  94. "query",
  95. "path",
  96. "url",
  97. "origin",
  98. "href",
  99. "subdomains",
  100. "protocol",
  101. "host",
  102. "hostname",
  103. "URL",
  104. "header",
  105. "headers",
  106. "secure",
  107. "stale",
  108. "fresh",
  109. "ips"
  110. ]