express.Static正在处理root URL请求。例如我想在Express从https://example.com重定向到https://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
})
如果有文件dist/index.html
,那将发生,因为这是express.static()
检索目录时所需的内容(在这种情况下为/
)。
您可以这样关闭该行为:
app.use(express.static(path.join(__dirname, 'dist'), { index : false }))
在此处记录:http://expressjs.com/en/4x/api.html#express.static