问题描述
实际上问题在标题中。 REST 端点定义如下:
fastify.get('/dir/:path',async (request,reply) => {
let res = await remote.getDir(request.params.path);
return { res: res}
})
一个电话就像
问题在于 Fastify 将路径参数视为 URL 的延续,并说:"Route GET:/dir// not found"
解决方法
斜线 /
被评估为路径段 written in the standard
要归档您的目标,您需要:
- 将
path
定义为查询参数 - 或定义路径参数,但以不使用
/
的格式对其进行编码,例如 base64
示例 1:
const fastify = require('fastify')()
fastify.get('/dir',{
schema: {
querystring: {
type: 'object',required: ['path'],properties: {
path: { type: 'string' }
}
}
}
},async function (request,reply) {
return request.query
})
fastify.listen(8080)
// call it with: http://localhost:8080/dir?path=/