Giter VIP home page Giter VIP logo

Comments (6)

nwoltman avatar nwoltman commented on August 11, 2024 9

Try setting Fastify's ignoreTrailingSlash option to true.

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

from fastify-static.

Eomm avatar Eomm commented on August 11, 2024 1

Released in 2.7.0

Thank you for your contribution!

from fastify-static.

jennyEckstein avatar jennyEckstein commented on August 11, 2024

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

from fastify-static.

mcollina avatar mcollina commented on August 11, 2024

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?

from fastify-static.

jennyEckstein avatar jennyEckstein commented on August 11, 2024

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.

from fastify-static.

mcollina avatar mcollina commented on August 11, 2024

That'd be rad, thanks!

from fastify-static.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.