简体   繁体   中英

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

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.

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