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
77 views
in Technique[技术] by (71.8m points)

javascript - Why there is no physical client/build folder when i run nodejs server with react app as frontend

I am using esausilva/example-create-react-app-express to create a base for my MERN project

Inside the ~/server.js file it was written

if (process.env.NODE_ENV === 'production') {
  // Serve any static files
  app.use(express.static(path.join(__dirname, 'client/build')));

  // Handle React routing, return all requests to React app
  app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
  });
}

To my understanding this mean if the express server is running in production mode then it will look into the client/build folder to serve the static file(i.e front-end code) and for any route request the server will send client/build/index.html file.

Inside the package.json i have the following npm run command

"scripts": {
    "client": "cd client && yarn start",
    "server": "nodemon server.js",
    "dev": "concurrently --kill-others-on-fail "yarn server" "yarn client"",
    "dev:server": "cd client && yarn build && cd .. && yarn start",
    "start": "node server.js",
    "heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
  },

my confusion is when i run yarn dev it run the server and react app concurrently(everything is perfect) but inside the client folder i don't see any physical build folder,why this is that happening, how express server understand which file to serve if it don't create the build folder first.

question from:https://stackoverflow.com/questions/65598274/why-there-is-no-physical-client-build-folder-when-i-run-nodejs-server-with-react

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

1 Reply

0 votes
by (71.8m points)

You need a build script so that it can generate the build folder, what the command yarn dev does is that it runs the development server. You can see more info on the react documentation.


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

...