express.static处理根URL请求

max*_*c00 5 node.js express

express.static正在处理根URL请求.例如,我想从https://example.comhttps://example.com/dashboard进行快速重定向.检查下面的情况,第一个工作,第二个不工作.我希望第二个也能奏效.谁知道为什么?

案例1(工程)

app.get('/', (req, res, next) => {
   res.redirect('/dashboard');
})    

app.use(express.static(path.join(__dirname, 'dist')))

app.get('/dashboard', (req, res, next) => {
   //do stuff
}) 
Run Code Online (Sandbox Code Playgroud)

案例2(对我不起作用)

app.use(express.static(path.join(__dirname, 'dist')))

//request doesn't come here 
app.get('/', (req, res, next) => {   
   res.redirect('/dashboard')
})

app.get('/dashboard', (req, res, next) => {
  //do some stuff
}) 
Run Code Online (Sandbox Code Playgroud)

rob*_*lep 8

如果有文件会发生这种情况dist/index.html,因为这是express.static()检索目录时所需的内容(在本例中/).

您可以像这样关闭该行为:

app.use(express.static(path.join(__dirname, 'dist'), { index : false }))
Run Code Online (Sandbox Code Playgroud)

记录在这里:http://expressjs.com/en/4x/api.html#express.static