Home > Blockchain >  Setting a base URI path with Fastify?
Setting a base URI path with Fastify?

Time:01-24

My app is hosted behind a proxy which automatically sets a base URI, e.g.

proxy.domainname.com/proxyAppNamespace/myFastifyApp

How can I make fastify aware of the proxyAppNamespace and myFastifyApp resource in the URL? This was pretty simple with Express I just needed to use:

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

How can I also do this in Fastify?

The problem I am having is that Fastify is assuming all of my resources are available right after the .com so I'm getting 404s. Do I want RoutePrefixing?

https://www.fastify.io/docs/latest/Reference/Routes/#route-prefixing

I was trying this but it seems like it wants the resources to be in a specific folder with those names..

Looks like Nextjs has a BasePath for this - https://nextjs.org/docs/api-reference/next.config.js/basepath

CodePudding user response:

I think I got what I wanted with this, it requires the fastify-static module. All of my assets are served out of a public folder.

var basePathPrefixStr = '/proxyAppNamespace/myFastifyApp'

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

CodePudding user response:

You find the fastify-static module, but if you need to add new application endpoints you should wrap all your application into a plugin:

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
})

  •  Tags:  
  • Related