简体   繁体   中英

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. 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. To be clear, it finds the index.html file and loads the html but not the bundle inside the index file. However if my index.html is automatically loaded via express as the index file my bundle is showing.

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.

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.

My index.html file

<!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.

I think the problem lies somewhere either with the Webpack bundle config or with 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. Hope this helps others in my situation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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