如何将文件或目录路径作为 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=/

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...