Kevin
Kevin

Reputation: 103

Is there a way to stop express from rendering files in a static directory?

The file server part of my application is similar to Nextcloud Files and I am storing the files in a folder that is set up in express with router.use(express.static('files'))

Problem is, when I upload an .html file with my application, when it redirects to /files res.redirect('/files') instead of rendering the files.pug template like it normally does and as specified in the router, it renders the newly uploaded .html file.

router.get('/files', (req, res) => {
    res.render('files', {
        someData: goingIn
    })
})

I've been searching but it seems more folks are stuck on serving that .html file and not many are trying to stop it. Perhaps there is a better method that I am missing. Is there a way to stop express from rendering files in a static directory?

Upvotes: 2

Views: 212

Answers (1)

Kevin
Kevin

Reputation: 103

I solved this by passing an option to express.static { index: false }

This disables directory indexing according to the docs https://expressjs.com/en/4x/api.html#express.static

const options = {
    index: false
}
router.use(express.static('files', options))

Upvotes: 3

Related Questions