hh54188
hh54188

Reputation: 15626

In node.js express , the default router always jump to index.html static file

My default router is :

app.get('/', function (req, res) {
    console.log("default");
    res.send("ok");
});

If I don't have a index.html in my static directory, the log and send will output as will,

but when I add index.html, the router will direct jump to the page, then I change the name index to another name, it doesn't jump again.

Why this happen? How can I change this setting things?

Upvotes: 2

Views: 5395

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146064

It's your middleware order and configuration that causes this behavior. You presumably are using the static middleware and by default that happens before the app.router middleware. The solution is either:

  • configure your static middleware to only serve exact file matches without any automatic index.html behavior
  • put app.user(app.router); before your static middleware

Upvotes: 5

Related Questions