A
A
Andrey Okhotnikov2020-02-09 12:26:46
Node.js
Andrey Okhotnikov, 2020-02-09 12:26:46

Express js fails React build?

Build React project (create-react-app) via yarn build. The assembly can be started via serve -s build (works correctly)

On the express server, I registered

app.use(express.static(path.join(__dirname + "../../client/build")));
            
            app.get("*", (req, res) => {
                res.sendFile(path.resolve(__dirname, "..", "..", "client", "build", "index.html"));
            });


When starting the server and entering the browser, the server gives this file, but in the id=root element, where the content should be added by means of react, it is empty.
Moreover, if you manually write something into the index.html file, it will be displayed.

Error in console
5e3fd05377372482029736.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Okhotnikov, 2020-02-09
@tsepen

It helped, replaced the above code with this

const root = require('path').join(__dirname, '..', '..', 'client', 'build')
            app.use(express.static(root));
            app.get("*", (req, res) => {
                res.sendFile('index.html', { root });
            })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question