使用Fastify设置基本URI路径



我的应用程序托管在一个代理后面,该代理自动设置基本URI,例如

proxy.domainname.com/proxyAppNamespace/myFastifyApp

如何让fastify知道URL中的proxyAppNamespace和myFastifyApp资源?这对Express来说非常简单,我只需要使用:

app.use('/proxyAppNamespace/myFastifyApp/', express.static(__dirname + '/public'));

我如何在Fastify中也能做到这一点?

我遇到的问题是,Fastify假设我的所有资源都在.com之后可用,所以我得到了404。我想要RoutePrefixing吗?

https://www.fastify.io/docs/latest/Reference/Routes/#route-预混合

我试过了,但它似乎希望资源位于具有这些名称的特定文件夹中。。

看起来Nextjs对此有一个BasePath-https://nextjs.org/docs/api-reference/next.config.js/basepath

我想我得到了我想要的,它需要fastify静态模块。我所有的资产都是从一个公用文件夹中提供的。

var basePathPrefixStr = '/proxyAppNamespace/myFastifyApp'
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: basePathPrefixStr,
wildcard: true,
});

您可以找到fastify-static模块,但如果需要添加新的应用程序端点,则应将所有应用程序封装到一个插件中:

const basePathPrefixStr = '/proxyAppNamespace/myFastifyApp'
fastify.register(function plugin (instance, opts, next) {
instance.get('/my-endpoint', function (req, reply) {})
// all your APIs go here
next()
}, {
prefix: basePathPrefixStr
})

最新更新