Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

react ssr - Express does not serve static files

I have built react-ssr with webpack, express and react. I build react files in different folder called build. Than I bundle my server side. Than I want to serve the files inside build/ folder. But it does not work.

https://something.com/static/css/main.chunk.css
https://something.com/static/js/main.chunk.js

These urls returns with 404

https://something.com/build/static/css/main.chunk.css
https://something.com/build/static/js/main.chunk.js

These urls work.

/server/index.js I tried two express static use function.

app.use('/static',express.static('../build/static'));
app.use(express.static(build'));

None of these worked. As you see below I am bundling server side into server_build/index.js. And I am calling node server_build/index.js which causes build/ folder not in the root.

My file structure is something like this.

build
|_static ==> This is where I got 404 
  |_js
    |_main.chunk.js
    |_main.chunk0.js
  |_css
    |_main.chunk.css
    |_main.chunk0.css
server_build
|_index.js ==> This is bundled server side express code. 
|_main.css
server
|_index.js ==> This is where my unbundled index js.

I am probably missing something here but it just does not work. This was working in my Macbook localhost, now It does not work on my Linux dedicated server. Whether there is a path issue or node execution root issue. Please help

question from:https://stackoverflow.com/questions/65918252/express-does-not-serve-static-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...