9

I am using Express 4.12.3 to serve out static files for a web site. I want to be able to navigate to example.com/mypage which would retrieve /mypage.html. In other words I want to be able to pull up the page without having to type in the .html extension in the URL.

My code looks like this:

var express     = require('express');
var app         = express();
var server      = require('http').Server(app);

app.use(express.static(__dirname + '/public'));

server.listen(4000);

I can access my page in the browser while using the .html extension but get a not found when dropping the extension. Any ideas how to configure my express server to allow this?

1 Answer 1

23

You can set the extensions option to include fallback file extensions to try:

app.use(express.static(__dirname + '/public', {
  extensions: ['html']
}));
Sign up to request clarification or add additional context in comments.

2 Comments

That was the ticket. Exactly what I was looking for. Kind of feel sheepish, since its such a simple solution. Wonder how i missed that in the documentation.
And is there some simple way to account for trailing slashes without needing to use a dependency like express-slash?

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.