How do connect to multiple APIs with React App?
Create-react-app with Express backend, using http-proxy-middleware.
Demo bug : https://github.com/svgpubs/nodeproxy.git
I am using http-proxy-middleware
to try to connect a demo React App to two different servers: one external website https://api.coingecko.com/api/
and one internal site http://localhost:3001/
It works for the external website. However, localhost:3001 connection doesn't work.
I can connect to the localhost:3001 if I don't use http-proxy-middleware (by adding 'proxy: 'http://localhost:3001'"
in package.json
) - however, then I can only have one proxy.
Here is the app running: as you can see, there is no response from localhost:3001
Errors: I've tried so many different variations. I either get a cors block from the browser, or the localhost api returns index.html file from public/index.html - resulting in a json parsing error in the browser. On the server, depending on the exact route for localhost endpoint, I sometimes get 50+ lines of this error:
Error occurred while trying to proxy request /localhostapi/users from localhost:3001 to http://localhost:3001/ (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
How do I setup the server and proxy so that App.js can connect to both localhost:3001 routes AND external APIs?
Here is my App.js
import React, { useEffect, useState } from "react";
import "./App.css";
function App() {
const [externalServerResponse, setExternalServerResponse] = useState(
"no response"
);
const [localhostServerResponse, setLocalhostServerResponse] = useState(
"no response"
);
const getExternalAPI = async () => {
console.log("calling external api from client");
const result = await fetch("http://localhost:3001/api_to_external_website");
console.log("result", result);
const data = await result.json();
console.log("data", data);
setExternalServerResponse(JSON.stringify(data[0]));
};
const getLocalHostAPI = async () => {
console.log("calling localhost api from client");
const result = await fetch("/localhostapi"); //I've tried many syntax variations
console.log("result", result);
const data = await result.json();
console.log("data", data);
setLocalhostServerResponse(JSON.stringify(data));
};
useEffect(() => {
getExternalAPI();
getLocalHostAPI();
}, []);
return (
<div className="App">
<div style={{ marginTop: "3em", marginBottom: "1em" }}>
<h2>
Response from{" "}
<code>
<i>www.api.coingecko.com/api</i>
</code>
:
</h2>
</div>
<div>{externalServerResponse}</div>
<div style={{ marginTop: "3em", marginBottom: "1em" }}>
<h2>
Response from{" "}
<code>
<i>localhost:3001</i>
</code>{" "}
:{" "}
</h2>
</div>
<div>{localhostServerResponse}</div>
</div>
);
}
export default App;
Here is server.js
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
const port = 3001;
const app = express();
app.use(
"/api_to_external_website",
createProxyMiddleware({
target:
"https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&order=market_cap_desc&per_page=100&page=1&sparkline=false",
headers: {
accept: "application/json",
method: "GET",
},
changeOrigin: true,
})
);
app.use(
"/localhostapi",
createProxyMiddleware({
target: `http://localhost:${port}/`,
headers: {
accept: "application/json",
method: "GET",
},
changeOrigin: true,
})
);
app.get("/", (req, res) => {
console.log("localhost:3001 api is running");
const data = { result: `Success! from localhostapi on localhost:${port}!!` };
res.send(JSON.parse(data));
});
app.listen(port, function () {
console.log(`server running now.. ${port}`);
});
How do I set up my server and proxy so that my App.js can get both localhost:3001 routes AND external APIs?
Instructions to run app:
In one terminal: make a folder, clone nodeproxy app, install dependencies, then run server
mkdir newfolder
cd newfolder
git clone https://github.com/svgpubs/nodeproxy.git
npm install
node server.js
Then, keeping the first terminal running, open a second terminal window: go to that same folder and start the react app.
cd newfolder
npm start
List of things I've tried:
Using additional package.json attribute "proxy: 'localhost:3001'
src/setupProxy.js
const { createProxyMiddleware } = require("http-proxy-middleware");
module.exports = function(app) {
app.use(
"/localhostapi",
createProxyMiddleware({
target: `http://localhost:${port}/`,
headers: {
accept: "application/json",
method: "GET",
},
changeOrigin: true,
})
);
}
- changing fetch and app.use syntax
[![enter image description here][3]][3]