【发布时间】:2025-12-17 08:55:01
【问题描述】:
我可以像这样提供静态资产(由npm run building React 源代码创建):
app.use('/', express.static(path.join(__dirname, 'apps', 'home', 'build')))
如果我想保护 URL 及其静态资产,我可以这样做:
app.use(function(req, res, next) {
if (!req.isAuthenticated()) {
res.redirect('/login');
}
else {
app.use('/profile', express.static(path.join(__dirname, 'apps', 'profile', 'build')))
next();
}
});
如果我不在那里调用next(),当我在/profile 进行身份验证尝试时程序会挂起。
接下来会调用哪些中间件/路由?如果没有身份验证,app.use(express.static(...)) 在没有next() 的情况下提供静态资产似乎没有问题。为什么我现在需要它?我没有为 /profile 或类似的东西定义 GET 路由。
【问题讨论】:
标签: javascript reactjs express