B
B
B1dloKoder2020-08-18 22:02:30
Express.js
B1dloKoder, 2020-08-18 22:02:30

How to properly organize the distribution of statics on express.js, taking into account react-router?

I'm trying to write web applications using react and express. Came across some oddities.

This is how I distribute static.

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


To make it work with react-router I do this

app.use('*', (req, res) => {
  res.sendFile("index.html", { root: __dirname + "/../public/"});
})


The first problem is that different browsers somehow "perceive" file paths differently. For example, chrome sees files included in index.html.
5f3c217d53c25087069389.png
Firefox also works,
but Yandex does not have a browser.
5f3c21e2d103b285978332.png
5f3c21ed04912021148414.png

The second problem is that when distributing files, for some reason, the path from the address bar is taken into account. Sorry, maybe I'm wording it wrong, below are screenshots for clarity. If you do not reload
the page, then the routing seems to work normally, except for the error, the nature of which is not entirely clear to me :
5f3c23e3d9973422579651.jpeg
5f3c24422a052837348606.jpeg

From this follows the question, how to properly configure the distribution of statics in express? Prescribe each path when I distribute statics? Looks so so. But I can't even do that until I solve the first problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
B1dloKoder, 2020-08-19
@B1dloKoder

The problem was with the name of the paths in index.html
after I renamed the folders from css and js to javascript and stylesheet everything worked.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question