0

Actually the question is in the header. A REST endpoint definition is below:

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

And a call is like

http://127.0.0.1:3000/dir//

The problem is that Fastify treats the path parameter as continuation of the URL, and says: "Route GET:/dir// not found"

Dmitry
  • 727
  • 1
  • 8
  • 34

1 Answers1

1

The slash / is evaluated as a path segment as written in the standard

To archive your goal you need to:

  1. define the path as a query parameter
  2. or define the path parameter but encode it in a format that doesn't use /, like the base64

Example 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=/
Community
  • 1
  • 1
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • Could you please specify exactly what do you mean by 'define as query parameter'? I tried to use `...?path=:path` – Dmitry Feb 01 '21 at 08:24