【问题标题】:Node js express not loading webpack bundle js if redirected to index如果重定向到索引,Node js express 不会加载 webpack bundle js
【发布时间】:2018-03-23 20:32:14
【问题描述】:

我正在使用 app id 作为护照解决方案向网站添加登录功能。护照位工作正常。

然而,问题是当我重定向到我的捆绑包所链接的文件 index.html 时,它不会仅显示捆绑包的 html。需要明确的是,它会找到 index.html 文件并加载 html,但不会加载索引文件中的包。但是,如果我的 index.html 是通过 express 自动加载为我的包显示的索引文件。

因此,如果我转到页面基本 url,捆绑包将显示,但如果我进入 /login,然后将我的网站重定向到 /public/index.html,捆绑包不会显示。

这个

app.get('/*',function(req,res,next){
     console.log("is user; ")
     console.log(req.user)
     if (req.user) {
         res.sendFile(__dirname + '/public/index.html'); 
     } else {
         res.redirect("/login")
      }
})

const LANDING_PAGE ="/public/index.html";
app.get("/login", passport.authenticate(WebAppStrategy.STRATEGY_NAME, {successRedirect : LANDING_PAGE, forceLogin: true}));

这不会显示捆绑包,而是使用

app.use(express.static(__dirname + '/public')); 

自动加载 index.html 并显示捆绑代码。

我的 index.html 文件

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo react</title>
    <link rel="shortcut icon" href="filer/DL_Logo.svg" type="image/svg">
</head>

<body>
    <h1>Hello this is kinda working...</h1>
    <div id="container"></div>
    <script src="/dist/bundle.js" type="text/javascript"></script>
    <!-- Resource jQuery -->
</body>

</html>

我没有控制台错误,网络选项卡显示它加载了捆绑 JS 文件。

我认为问题出在 Webpack 捆绑配置或 Express 的某个地方。

【问题讨论】:

  • 那是因为你没有包含你的 bundle 文件,我猜它目前位于 dist

标签: javascript node.js express webpack passport.js


【解决方案1】:

我解决了。问题不在服务器端,而实际上与反应路由有关。我的反应路由器 exakt 路径使组件永远不会为任何其他 url 安装。希望这可以帮助其他人在我的情况下

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多