Home > front end >  Problem serving index.html from Express server, but not when I run client separately
Problem serving index.html from Express server, but not when I run client separately

Time:03-28

I have an app that is made using ReactJS and ExpressJS. The Express server is responsible for getting data, and ReactJS for displaying it. When I run them separately, meaning, I go into the root folder and start the server, and then go into client and start the React app, everything works fine, but when I try to serve the static index.html file I get an error.

Uncaught TypeError: Cannot read properties of undefined (reading 'regular')

This is how I'm serving the static file:

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

CodePudding user response:

If your file structure is:

|root
   |client
      |build
   -server.js

then you need to specify the path as __dirname, "client", "build", "index.html" and use join instead of resolve.

  • Related