微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将文件或目录路径作为 REST API 参数传递给 Fistify 端点

如何解决如何将文件或目录路径作为 REST API 参数传递给 Fistify 端点

实际上问题在标题中。 REST 端点定义如下:

fastify.get('/dir/:path',async (request,reply) => {
    let res = await remote.getDir(request.params.path);
    return { res:  res}
})

一个电话就像

http://127.0.0.1:3000/dir//

问题在于 Fastify 将路径参数视为 URL 的延续,并说:"Route GET:/dir// not found"

解决方法

斜线 / 被评估为路径段 written in the standard

要归档您的目标,您需要:

  1. path 定义为查询参数
  2. 或定义路径参数,但以不使用 / 的格式对其进行编码,例如 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=/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。