有问题的路线是这样的:chats / 5de3e056c022b2b3252dab43 / messages,但我似乎找不到相对简单的方法来检索ID(5de3e056c022b2b3252dab43),但找不到它的位置(是的,我可以解析网址,但是我希望有更好的方法-符合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>"
}
答案 0 :(得分:1)
如果您有这样的自定义路线/chats/:id/messages
在您的控制器中,您将可以使用ctx.params.id