首页 > 解决方案 > 在 Fastify 中使用参数编写路由完整 URL

问题描述

有没有办法在 Fastify 中引用某个路由的完整路径?我的意思是如果我需要,考虑到服务器主机和过去的参数,得到它的完整路径。

我的情况:我有 2 条路线 - 上传和查看文件。

// Here is the module with my routes
const routes = async (fastify: FastifyInstance) => {
  fastify.get('/file/:id', async (req, reply) => { // [1]
    ... // here a can view a file by id
  });
  fastify.post('/file', async (req, reply) => {
    ... // uploading a the file
    const fileId = uploadFile.id;
    const viewFileURL = ...; // [2] Here I need to reference on route [1]

    return { success: true, src: viewFileURL };
  });
}

在 POST 路由 (2) 中,我想编写一个 URL,以便在文件上传后立即查看文件。

我的最终目标是当用户上传文件时,他可以从响应中获取链接并访问它以检查上传的文件。

标签: routesfastify

解决方案


推荐阅读