【发布时间】:2020-03-12 23:27:48
【问题描述】:
我的目标是
当用户进入特定页面时向我的控制台打印一条消息
无需编写 .html 扩展名即可访问页面。
如果我使用以下内容,其中 test.html 不是现有页面,我会看到
当用户尝试访问 /test 或 /test.html 页面时,我的控制台中的预期消息。
router.get(/^\/test(\.html)?$/, async (req, res) => {
console.log('User trying to access test.html page')
res.send('welcome to test page')
})
但如果我对现有页面执行相同操作 (/dashboard.html)
router.get(/^\/dashboard(\.html)?$/, async (req, res) => {
console.log('User trying to access dashboard.html page')
res.send('welcome to dashboard page')
})
当用户尝试访问/dashboard 时,我会在我的控制台中看到预期的消息,但是当他尝试访问/dashboard.html 时,页面只会加载而在我的控制台中看不到任何消息。
为什么会这样?
【问题讨论】:
-
添加设置快速服务器的代码。在这个路由器之前。
-
我已经这样做了。这些路线在
src/routers/user.js底部我使用module.exports = router然后在src/index.js我包括const userRouter = require('./routers/user')然后app.use(userRouter) -
我在你的帖子中看不到
标签: node.js express routing url-routing