1

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 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

yes, the backend server is done with nodejs and it has called all queries of sql for postgres, and it is online on ec2 aws, and i have make a new component in the existing, so how will i get submit a form data from my react component to postgres? do i need to run my server.js of backend locally also on 4000 port? can i use PostMan to check whether the data is being updated or not?
Well, you can use postman to make api calls to your backend, which will then query or update your database, but - as far as I know - you can't do it directly from postman to postgres. You send the form data to the backend, read the values there and update the database with them. After that, you can send a successful response back to the client. Your backend does not need to run on port 4000, nearly every other free port is also fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.