简体   繁体   English

Node JS Express如果重定向到索引,则不会加载Webpack bundle js

[英]Node js express not loading webpack bundle js if redirected to index

I'm using app id a passport solution to add a login function to the website. 我正在使用应用程序ID护照解决方案向网站添加登录功能。 The passport bit is working fine. 护照位工作正常。

The problem however is when i redirect to the file index.html where my bundle is linked, it wont show the bundle only the html. 但是问题是当我重定向到链接我的捆绑软件的文件index.html时,它不会仅显示html捆绑软件。 To be clear, it finds the index.html file and loads the html but not the bundle inside the index file. 为了清楚起见,它将找到index.html文件并加载html,但不加载索引文件中的包。 However if my index.html is automatically loaded via express as the index file my bundle is showing. 但是,如果我的index.html是通过express自动加载为索引文件的,则我的捆绑软件将显示出来。

So if I go to the page basic url the bundle will show, but if i go into my /login which then redirects my site to /public/index.html the bundle doesn't show. 因此,如果我转到基本网址页面,则该捆绑软件将显示,但是如果我进入/ login,然后将我的网站重定向到/public/index.html该捆绑软件将不会显示。

This 这个

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")
      }
})

and

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

This wont display the bundle, but using 这不会显示捆绑包,但是使用

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

automatically loads the index.html and that shows the bundle code. 自动加载index.html并显示捆绑代码。

My index.html file 我的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>

I have no console errors the network tab shows it loaded the bundle JS file. 我没有控制台错误,网络选项卡显示它已加载捆绑包JS文件。

I think the problem lies somewhere either with the Webpack bundle config or with Express. 我认为问题出在Webpack捆绑包配置或Express上。

I solved it. 我解决了 The problem was not on the server side but actually with react routing. 问题不在于服务器端,而在于反应路由。 I had on my react router exakt path which made the component never mount for any other url. 我在我的react router exakt路径上安装了该组件,因此该组件从不安装任何其他URL。 Hope this helps others in my situation 希望这对我的情况有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM