ultimo_frogman
ultimo_frogman

Reputation: 91

Use external REST API with ReactJS and NodeJS

I am still learning about ReactJS and NodeJS.

My devs have created a boilerplate with create-react-app We have an External REST API service , and we want to make calls (GET, PUT ...) to their APIs and retrieve and put data.

My question is, ReactJS can also consume REST APIs (external ones, from another web site). Why do I need NodeJS then? I think ideal would be that NodeJS makes those calls for ReactJS and makes server side rendering.

What is the best practice here with this stack. I do not need (or maybe do I?) to buld internal APIs with ExpressJS, since I have external ones from another web service provider which retrieves data for me out of their data store.

Can someone elaborate on the best practice here ?

Thanks in advance.

Upvotes: 0

Views: 2918

Answers (1)

Red Baron
Red Baron

Reputation: 7652

React can easily make API calls. this is typically done through componentDidMount method or in useEffect if using hooks.

You can of course use a backend to make these requests too like you said with NodeJS. but whether you do or don't is completely up to you and your use case

typically you would use NodeJS and a backend to help separate out logic, especially if you need to make calls to a database and stuff.

another good reason would be if you are handling sensitive information you would want to do this server side and not client side.

so in summary, it's completely up to you and what you need. personally I use react with lambda/api gateway in aws to separate logic out

example node js endpoint (returning an array to react)

app.get('/api/my-route', (req, res, next) => {
    const array = [1,2,3,4,5]
    res.json(array)
})

Upvotes: 2

Related Questions