ctx 默认的对象
- ctx.request,请求
- ctx.response,响应
- ctx.app,Application实例
- ctx.service, Server
- ctx.render, 渲染模板引擎
- ctx.cookie
- ctx.session
- ctx.curl, httpClient请求
常用的 ctx.key
const ctx = [
"request",
"response",
"app",
"req",
"res",
"onerror",
"originalUrl",
"starttime",
"matched",
"captures",
"params",
"routerName",
"_matchedRouteName",
"routerPath",
"_matchedRoute"
]
{
request: {
method: 'GET',
url: '/users/list?id=23&user=lucy',
header: {
'user-agent': 'PostmanRuntime/7.28.4',
accept: '*/*',
'postman-token': '01ced84c-c56f-4df5-a813-20f67f8732cc',
host: '127.0.0.1:6001',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive',
cookie: 'csrfToken=SGl9XQed64inFmoCp-QjQnlL'
}
},
response: {
status: 404,
message: 'Not Found',
header: [Object: null prototype] { vary: 'Origin' }
},
app: {
env: 'local',
name: 'egg-bff',
baseDir: '/Users/lulongwen/Documents/Project/icestark/egg-bff',
subdomainOffset: 2,
config: '<egg config>',
controller: '<egg controller>',
httpclient: '<egg httpclient>',
loggers: '<egg loggers>',
middlewares: '<egg middlewares>',
router: '<egg router>',
serviceClasses: '<egg serviceClasses>'
},
originalUrl: '/users/list?id=23&user=lucy',
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
}
全部的 ctx
[
"request",
"response",
"app",
"req",
"res",
"onerror",
"originalUrl",
"starttime",
"matched",
"captures",
"params",
"routerName",
"_matchedRouteName",
"routerPath",
"_matchedRoute",
"securityOptions",
"isSafeDomain",
"nonce",
"csrf",
"ensureCsrfSecret",
"rotateCsrfSecret",
"assertCsrf",
"safeCurl",
"acceptJSONP",
"createJsonpBody",
"locale",
"cleanupRequestFiles",
"saveRequestFiles",
"multipart",
"getFileStream",
"render",
"renderView",
"renderString",
"view",
"validate",
"info",
"cookies",
"httpclient",
"curl",
"router",
"helper",
"getLogger",
"logger",
"coreLogger",
"locals",
"state",
"runInBackground",
"_runInBackground",
"acceptJSON",
"queries",
"accept",
"ip",
"realStatus",
"user",
"unsafeRedirect",
"__",
"__getLocale",
"__getLocaleOrigin",
"__setLocale",
"gettext",
"inspect",
"toJSON",
"assert",
"throw",
"attachment",
"redirect",
"remove",
"vary",
"has",
"set",
"append",
"flushHeaders",
"status",
"message",
"body",
"length",
"type",
"lastModified",
"etag",
"headerSent",
"writable",
"acceptsLanguages",
"acceptsEncodings",
"acceptsCharsets",
"accepts",
"get",
"is",
"querystring",
"idempotent",
"socket",
"search",
"method",
"query",
"path",
"url",
"origin",
"href",
"subdomains",
"protocol",
"host",
"hostname",
"URL",
"header",
"headers",
"secure",
"stale",
"fresh",
"ips"
]