Kay*_*ast 6 routing node.js express
我有这些文件的公开目录index.html,并index-08.html在里面.
使用下面的代码,我希望index-08.html能够提供服务.但相反,请index.html使用浏览器请求localhost:3000
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.sendFile('public/index-08.html');
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我将文件名更改index.html为其他内容,比如说not-index.html,则会提供正确的文件index-08.html.
你能帮我理解为什么会这样吗?
Ole*_*leg 12
这是因为您app.use(express.static) 之前 声明过app.get('/').Express按照声明的顺序检查路由,并且由于index.html是static中间件使用的默认文件名,它显示index.html内容.
为了解决这个问题,你可以要么把app.use(express.static)后app.get('/'),或设置index的属性static第二个参数不存在的文件(false似乎不工作):
app.use(express.static(path.join(__dirname, 'public'), {index: '_'}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4770 次 |
| 最近记录: |