8

The goal here is to serve my static site (generated with nuxtjs) with url without trailing slash.

I want to serve foo.html with url /foo To do that I am using express static with extensions option

app.use(express.static(__dirname + '/public', { extensions: 'html' }))

That is working fine except when an html file has the same name as a folder. Let's consider this file tree :

foo.html
bar.html
bar/baz.html

/foo will serve foo.html

/bar/baz will serve baz.html

but /bar will redirect to /bar/

I tried to deactivate the redirect option that way :

app.use(express.static(__dirname + '/public', { extensions: 'html', redirect: false }))

Now /bar is not redirecting anymore but the file bar.html is still not served !

Express is just moving to next()

I was able to serve bar.html adding this after other routing :

app.use(function(req, res, next) {
    var file = __dirname + '/public' + req.path + '.html'
    fs.exists(file, function(exists) {
        if (exists) res.sendFile(file)
        else next()
    })
})

But I feel this should not be the right way to do that, I should be able to serve all my files with static.

5
  • I don't understand exactly what you're trying to do, but when express.static() finds that /foo matches a foo directory in the static file hierarchy, it then looks inside that directory for an index.html file and, if found, it will serve that. Perhaps you can use that to solve your issue.
    – jfriend00
    May 25, 2018 at 4:46
  • Well to be exact when : express.static() finds that /foo matches a foo directory in the static file hierarchy it will redirect to /foo/, and then /foo/ request will be served with /foo/index.html but because of that redirect url will be /foo/ with the trailing slash. The thing is my site generated with nuxt js that work as a single page app has visible href without trailing slashes therefore it will look like all my links are pointing to 301 redirects. And my boss believe it's bad (because his SEOs tools will show some red).
    – Felwin
    May 25, 2018 at 5:04
  • How many of these directory URLs do you have? You could just define manual routes for them that do exactly what you want.
    – jfriend00
    May 25, 2018 at 5:06
  • so there is no express solution for this? is there?
    – Ayyash
    Nov 8, 2022 at 16:19
  • looks like an open issue on express: github.com/expressjs/serve-static/issues/138
    – Ayyash
    Nov 8, 2022 at 16:57

1 Answer 1

0

From Express 4.8.0, you can use res.sendFile as an alternative to express.static. It uses the same backing code, and supports the same features such as HTTP cache support, content-type headers, etc.

const root = path.join(__dirname, '/public');
app.use((req, res, next) => {
  const file = req.url + ".html";
  fs.exists(path.join(root, file), (exists) =>
    exists ? res.sendFile(file, {root}) : next()
  );
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.