如何在 Strapi 中获取 URL 参数?
How to get a URL parameter in Strapi?
有问题的路线是这样的:chats/5de3e056c022b2b3252dab43/messages 但我似乎无法找到一种相对简单的方法来检索 ID (5de3e056c022b2b3252dab43) 并且无处可寻(是的,我可以解析 url 但我希望有更好的方法 - 类似于 req.params.id)
CTX 对象基本上是这样的:
{
"request": {
"method": "GET",
"url": "/chats/5de3e056c022b2b3252dab43/messages",
"header": {
"authorization": "Bearer ******",
"user-agent": "PostmanRuntime/7.19.0",
"accept": "*/*",
"cache-control": "no-cache",
"postman-token": "e320fdc2-81a5-4f36-a4d1-ec621188e27d",
"host": "localhost:1337",
"accept-encoding": "gzip, deflate",
"connection": "keep-alive"
}
},
"response": {
"status": 200,
"message": "OK",
"header": {
"vary": "Origin",
"content-security-policy": "img-src 'self' http:; block-all-mixed-content",
"strict-transport-security": "max-age=31536000; includeSubDomains",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "1; mode=block",
"content-type": "application/json; charset=utf-8",
"x-powered-by": "Strapi <strapi.io>"
}
},
"app": {
"subdomainOffset": 2,
"proxy": false,
"env": "development"
},
"originalUrl": "/chats/5de3e056c022b2b3252dab43/messages",
"req": "<original node req>",
"res": "<original node res>",
"socket": "<original node socket>"
}
如果您有这样的自定义路线/chats/:id/messages
在您的控制器中,您可以使用 ctx.params.id
访问聊天 ID
有问题的路线是这样的:chats/5de3e056c022b2b3252dab43/messages 但我似乎无法找到一种相对简单的方法来检索 ID (5de3e056c022b2b3252dab43) 并且无处可寻(是的,我可以解析 url 但我希望有更好的方法 - 类似于 req.params.id)
CTX 对象基本上是这样的:
{
"request": {
"method": "GET",
"url": "/chats/5de3e056c022b2b3252dab43/messages",
"header": {
"authorization": "Bearer ******",
"user-agent": "PostmanRuntime/7.19.0",
"accept": "*/*",
"cache-control": "no-cache",
"postman-token": "e320fdc2-81a5-4f36-a4d1-ec621188e27d",
"host": "localhost:1337",
"accept-encoding": "gzip, deflate",
"connection": "keep-alive"
}
},
"response": {
"status": 200,
"message": "OK",
"header": {
"vary": "Origin",
"content-security-policy": "img-src 'self' http:; block-all-mixed-content",
"strict-transport-security": "max-age=31536000; includeSubDomains",
"x-frame-options": "SAMEORIGIN",
"x-xss-protection": "1; mode=block",
"content-type": "application/json; charset=utf-8",
"x-powered-by": "Strapi <strapi.io>"
}
},
"app": {
"subdomainOffset": 2,
"proxy": false,
"env": "development"
},
"originalUrl": "/chats/5de3e056c022b2b3252dab43/messages",
"req": "<original node req>",
"res": "<original node res>",
"socket": "<original node socket>"
}
如果您有这样的自定义路线/chats/:id/messages
在您的控制器中,您可以使用 ctx.params.id