需要了解expressjs为什么redirect到index.html

我有以下服务器文件,使用快速:

var express = require('express'); var app = express(); var port = process.env.PORT || 8080; app.listen(port); console.log('Listening on port: ' + port); // get an instance of router var router = express.Router(); app.use('/', router); app.use(express.static(__dirname + "/")); // route middle-ware that will happen on every request router.use(function(req, res, next) { // log each request to the console console.log(req.method, req.url + " logging all requests"); // continue doing what we were doing and go to the route next(); }); // home page route for port 8080, gets executed when entering localhost:8080 // and redirects to index.html (correctly and as expected) router.get('/', function(req, res) { console.log("routing from route") res.redirect('index.html'); }); // This gets executed when my url is: http://localhost:8080/test // and redirects to index.html (the questions is why!? I thought // all the requests to root route would be caught by the router instance app.get('*', function(req, res){ console.log('redirecting to index.html'); res.redirect('/index.html'); }); 

看上面的代码和我的意见,我不明白为什么

 app.get('*', function(){...}) 

当URL是localhost:8080/index.html时不会被执行,但当URL是localhost:8080/test时会被执行
即使这是我所希望的行为,但我不确定这是为什么呢?
我没有在根目录中的“test.html”页面。 还有一件事,index.html确实加载了其他脚本,所以我期望

 app.get('*', function(){...}) 

也是为了得到这样的请求而执行的,因为它应该是所有的东西,但事实并非如此。

是否app.use('/', router)意味着任何具有单个字符“/”的路由应由路由器实例处理(只要不是静态文件)? 所以“http:localhost:8080”被解释为“ http:// localhost:8080 / ”?

我将不胜感激任何解释。

这条线 –

app.use(express.static(__dirname + "/"));

将首先运行。 它会看到index.html存在并静态地提供该文件。