Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Express - Show URL without using html extension

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?

like image 970
risingtiger Avatar asked May 20 '15 19:05

risingtiger


1 Answers

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

app.use(express.static(__dirname + '/public', {
  extensions: ['html']
}));
like image 102
mscdex Avatar answered Sep 27 '22 22:09

mscdex