2017-09-13 70 views
1

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

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

改变DIST到/ DIST应该使其工作 – itsundefined

回答

3

,如果有一个文件dist/index.html这会发生,因为这是express.static()看起来检索时一个目录(在这种情况下/)。

你可以把这种行为关闭这样的:

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

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

+0

很好的答案! @robertklep – turmuka

+0

哇...它像魅力一样工作。谢谢@robertklep – maxcc00

+0

你认为它可能有什么不需要的影响,或没有? – maxcc00