2017-09-13 68 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