I am building a simple project using React and I want it to be connected to a database (mongodb Atlas). however, I can only connect to my database using Node. Is it possible to run both Node and React on the same port 3000?
I also use Express together with Node because I find it very convenient using ejs files when making new pages with the same navBar and footer.
Is it possible to run these 3 guys together in one port?
Yes and no. In production Express will serve a static route pointing to your build folder, on the same port. In development mode, Express and React will need to separate ports. You can easily achieve that by setting Express on port 5000 and React on port 3000. Then you will need to declare this "proxy": "http://localhost:5000", in your React package.json. You can even run both Express and React simultaneously in one command if you install the package "concurrently".
You can proxy your api by adding the following proxy field to your package.json in your react project
"proxy": "http://localhost:4000"
Now in the frontend application you can make fetch API call using the app port e.g.
fetch('/api/user') // this calls -> http://localhost:4000/api/user
Read more here:: Proxying API Requests in Development
Well, it's senseless to have same port for different server. However, you want to replace ejs and use react inside node server, then you can host the project in single port. But it will be difficult for everyone, the developer, the users.
So, use different project directory for serving client and the server. If you are facing some issue with the connection to the database, and feeling the one port is only working, you can change the port for them.
However, I think this post should help you to connect mongodb?