Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional trailing "/" after prefix #122

Closed
jennyEckstein opened this issue Apr 16, 2020 · 6 comments
Closed

Optional trailing "/" after prefix #122

jennyEckstein opened this issue Apr 16, 2020 · 6 comments

Comments

@jennyEckstein
Copy link
Contributor

馃悰 Bug Report

I am trying to serve static content over /docs prefix, but being redirected to /docs/ since trailing slash is appended by default.

To Reproduce

Register route with prefix that does not have trailing /, start application, assuming everything done right static content in docs folder will be available at /docs/ instead of /docs.

app.register(fastifyStatic, {
    root: path.join(__dirname, 'docs'),
    prefix: '/docs'
  });

Expected behavior

Static content be available at /docs instead of /docs/.

Your Environment

  • node version: 12
  • fastify version: 2.13.1
  • os: Linux
@nwoltman
Copy link
Contributor

Try setting Fastify's ignoreTrailingSlash option to true.

const fastify = require('fastify')({
  ignoreTrailingSlash: true
})

@jennyEckstein
Copy link
Contributor Author

Try setting Fastify's ignoreTrailingSlash option to true.

const fastify = require('fastify')({
  ignoreTrailingSlash: true
})

This won't work in my use case since, I am not using fastify directly, rather building an object and passing it to a module that creates fastify. Can the same flag be implemented in fastify-static?

const fastifyStatic = require('fastify-static');
async function registerRoutes(app) {
  app.register(fastifyStatic, {
    prefix: '/docs',
    root: path.join(__dirname, '../docs')
  });
  app.get('/foo', async (req, res)
}
// external module takes function to build routes
buildRoutes(registerRoutes);

@mcollina
Copy link
Member

Unfortunately that option is specific to fastify router, so you cannot set it on the module itself.

fastify-static/index.js

Lines 125 to 130 in e1ad93b

if (opts.redirect === true && prefix !== opts.prefix) {
fastify.get(opts.prefix, schema, function (req, reply) {
const parsed = url.parse(req.raw.url)
reply.redirect(301, parsed.pathname + '/' + (parsed.search || ''))
})
}
is the code that does the actual logic. Have you tried putting prefix: false?

Would you like to send a PR for a different option that implement what you need?

@jennyEckstein
Copy link
Contributor Author

So, I tried the option ignoreTrailingSlash: true and it still appends trailing slash :sad

However, if I modify

const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')

to

const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : opts.prefix;

My static content is served at /docs without trailing slash, just like I need. I can make a PR to make it optional with the flag.

@mcollina
Copy link
Member

That'd be rad, thanks!

@Eomm
Copy link
Member

Eomm commented Apr 20, 2020

Released in 2.7.0

Thank you for your contribution!

@Eomm Eomm closed this as completed Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants