Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs sendFile not loading webpack bundle

I'm using react and webpack to bundle the files and linking that to index.html like so,

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

This is working when i enter the standard url because express automatically finds the index.html and loads it. However if i try to use res.SendFile to the index.html file it only loads the html content, the H1 tag ,but does not display the rest of my react content that lays inside the bundle. This is working (Finding automaticly index.html),

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

This does not work

res.sendFile(__dirname+ "/public/index.html")
like image 622
danielo Avatar asked May 03 '26 19:05

danielo


1 Answers

I have had the same problem. The problem is not in node as it seems but actually in your react router and with your paths that render the components. Either your paths are wrong or you have the "exact" path that stops everything from rendering if it is not exactly that url.

like image 121
Adam Lagevik Avatar answered May 06 '26 09:05

Adam Lagevik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!