如何在fastify Nodejs中访问url

问题描述 投票:0回答:1

我正在尝试从 fastify 服务器访问部分 URL。

例如,网址如下所示:

http://localhost:3001/query_tile/10/544/336

在 fastify 服务器中,我需要 z/x/y 的值。我正在尝试做这样的事情:

interface BodyType {
    z : string
    x : string
    y : string

  }
  

fastify.get('/query_tile/:z/:x/:y', async (request : FastifyRequest<{ Body: BodyType }>, reply) => {

    const { z, x, y } = request.body;
    console.log(z,x,y, request.body)
// ....
}


但作为回报,我收到此错误消息:

{"statusCode":500,"error":"Internal Server Error","message":"Cannot destructure property 'z' of 'request.body' as it is undefined."}

访问这些值的正确方法是什么?

非常感谢!!

node.js typescript fastify
1个回答
0
投票

找到了:

interface BodyType {
    z : string
    x : string
    y : string
  }
  interface Querystring {
    useCache : string
  }
  

fastify.get('/query_tile/:z/:x/:y', async (request : FastifyRequest<{ Params: BodyType, Querystring : Querystring }>, reply) => {
    const { z, x, y } = request.params;
    const useCache = request.query.useCache !== 'false'; // use cache by default
...
© www.soinside.com 2019 - 2024. All rights reserved.