express.static处理根url请求
express.static正在处理根url请求。 例如,我想从https://example.com到https://example.com/dashboard进行快速redirect。 检查下面的情况,第一个工作,第二个不。 我希望第二个也能工作。 有人知道为什么
案例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()
在检索目录(在本例中为/
)时会查找的内容。
你可以像这样closures这个行为:
app.use(express.static(path.join(__dirname, 'dist'), { index : false }))
logging在这里: http : //expressjs.com/en/4x/api.html#express.static