0

node v5 express 4.13.3

What I did:

I was serving out the index.hmtl file when the index route("/") was hit. I then set it the public folder to be sent out as a static file.

What Problem it Caused:

Now that it is static, my index route is no longer entered when the browser opens the index route e.g.(localhost:3000/)

Question:

Is that expected behavior? Is it a good idea to serve index.html statically ?

1 Answer 1

2

Is that expected behavior?

Yes. Express will pass a request through the list of middleware (including your own index route), and if one of those can handle the request, it will and the request will finish (it won't get passed to other middleware).

The static middleware can handle the request for the index (presumably because you have an index.html in your public directory), so the request will end there and not get passed to your handler as well.

The order in which requests are passed to middleware is something that you can control. If you want your own index handler to get a higher priority, you should declare it before the static middleware:

app.get('/', function(req, res) {
  ...
});

app.use(express.static(...));

Is it a good idea to serve index.html statically ?

If it's plain HTML and there's nothing else that needs to be done when it's requested, it's probably best to let the static middleware handle it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @robertklep! :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.