I’m no React developer, and I’ve been doing a docker course that uses a multi-stage build Dockerfile with node and nginx to dockerize a React app. Why is nginx needed? And why can’t we simply use npm start in production? Doesn’t it already start a server and exposes the port for React to run?
2
Answers
When you are ready with your react app and publish it (
npm run build
), you will get a bunch of statichtml
andjs
files. And somehow you want to serve them, this is where a webserver, for examplenginx
comes in. (You can use any other webserver, for example serve)You can serve with node, if you create a server, which can serve static files too, npm start is for debugging, it will use much more resources (ram, cpu) and the file size will be bigger (it will load much slower)
You are correct, there is really nothing stopping you from just doing
npm start
even for production. For development purposes using a Nginx server is kind of overkill. However, the story is different for production environments. There are many reasons to use a "proper" webserver. Here some points:npm build
to obtain minified and optimized code. This will reduce the file size of your application which will reduce storage, memory, processing and networking resources. The result ofnpm build
is a bunch a static files which can be served from any web server.I guess your course just sets up example cases that are also relevant for people wanting to create production ready systems.