I have a public directory with the files index.html
and index-08.html
in it.
With the code below, I expect index-08.html
to be served. But instead, index.html
gets served with a browser request of localhost:3000
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.sendFile('public/index-08.html');
});
But if I change the file name of index.html
to something else, say not-index.html
, then the correct file index-08.html
gets served.
Can you please help me understand why this happens ?
This is because you declared app.use(express.static)
before app.get('/')
. Express checks routes in the order they are declared, and since index.html
is a default filename which is used by static
middleware, it shows index.html
content.
To fix this you may either put app.use(express.static)
after app.get('/')
, or set index
property of static
second argument to non-existing file (false
doesn't seem to work):
app.use(express.static(path.join(__dirname, 'public'), {index: '_'}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With