0

I have a React frontend app with nodejs express backend I have deployed on AWS EC2 instance. Currently it is only serving the html file but not loading the js file correctly (it works correctly locally). I've looked up on stackoverflow and found a similar issue but I was not able to fix it.

Here is my server.js file:

const express = require("express");
const cors = require("cors");
const path = require("path");
const bodyParser = require("body-parser");
const jwt = require("jsonwebtoken");
let app = express();
const mysql = require('mysql');
const port = process.env.PORT || 5000

process.env.SECRET_KEY = 'secret';

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

if (process.env.NODE_ENV === "production") {
  app.use(express.static("client/build"));
}

app.use(express.static(path.resolve(__dirname, './client/build')));

// some routes here

// app.get(“*”, (req, res) => {res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));});

app.listen(port, () => {
    console.log("Server is running on port: " + port)
})

Here is my folder structure:

enter image description here

On "/" it looks up the html file from the build folder correctly but it was not able to locate the js files. How should I fix the issue? Thanks!

Edit index.html file, just the basic index.html from create-react-app template

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="theme-color" content="#000000">
  <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" type='text/javascript'>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap" type='text/javascript' />
  <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.
i
      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
  <title>Recommender System</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
    crossorigin="anonymous">
</head>

<body>
  <noscript>
    You need to enable JavaScript to run this app.
  </noscript>
  <div id="root"></div>
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
    crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
    crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
    crossorigin="anonymous"></script>
  <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
</body>

</html>
3
  • Do you have script and link tags in the HTML file? Commented Apr 21, 2020 at 19:41
  • @AvivLo please see above update. the html file is just the template file from create-react-app. I think the issue might be after the build it cannot locate js files for some reason. I'm not sure if I'm serving the routes correctly. Commented Apr 21, 2020 at 21:05
  • Look at the updated answer. I think I ran into a similar situation before. Commented Apr 21, 2020 at 21:13

1 Answer 1

0

You will have to build your react app first before running the node backend.

The problem here is that you have app.use(express.static(path.resolve(__dirname, './client/build'))); points to the ./client/build directory which does not exist if you start your node backend without having finished building your react app first.

So maybe try building the react app first, and start the node app after that.

Update:

Because you set the path in express as ./client/build, you can perhaps add ./ before the routes, you put in the src attribute of your script tags. Like ./static/xx.js

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer! In my package.json for server it has start: npm run build-client && node server so it creates the directory. Also it can grab the html which is also in the directory, so /build should exist.
Hmm, strange. So how are you loading those js files?
In the build folder (after build step) under /static/js i can see the js files and it is inserted into the html file as a link. But it isn't loaded correctly, the errors happened in this
I just realized that I was the person answering that question yesterday.
I'm a little confused. I didn't set the link to the js files, it is added during build process in build/static is there any way i can modify the server.js file to serve them differently?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.