express.static处理根URL请求

时间:2017-09-13 17:44:41

标签: node.js express

express.static正在处理根网址请求。 例如我想在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
}) 

案例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
}) 

1 个答案:

答案 0 :(得分:8)

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

你可以这样关掉这个行为:

app.use(express.static(path.join(__dirname, 'dist'), { index : false }))

此处记录:http://expressjs.com/en/4x/api.html#express.static

相关问题