gpt4 book ai didi

node.js - express.static 处理根 url 请求

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:34 25 4
gpt4 key购买 nike

express.static 正在处理根 url 请求。例如我想从 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

关于node.js - express.static 处理根 url 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com