admin管理员组

文章数量:1221312

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/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?

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?

Share Improve this question asked May 20, 2015 at 19:09 risingtigerrisingtiger 8791 gold badge11 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 23

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

app.use(express.static(__dirname + '/public', {
  extensions: ['html']
}));

本文标签: javascriptNodeJS ExpressShow URL without using html extensionStack Overflow