I have a doubt, suppose, I made a React project using npx create-my-app myProject and in this public folder, I have several folders having NodeJS for Postgres database. so, my question is if I have to load data from postgres to react component, how will I do it? can I do it using axios? if not then how? if yes, how will I test it? suppose, my backend nodejs postgres is online, and I am using my react on local:3000, how will I test it, if postgres has get function is like /api/xyz ?
1 Answer
You really should not do this in the Frontend (=React) part of you application.
This would open the door to some serious security issues, as you would have to establish a connection to the database from the client, and in order do so, have to save the databases credentials there. It would be relatively easy for any attacker to get this data.
Basically, create a backend server (maybe with nodejs), create a connection there to postgres, fetch the requested data there and send it to the client. You can use axios for that or any other http library like fetch. Your frontend can run on localhost:3000 and the backend on localhost:4000. Here is a tutorial you can follow.