express – proxy requests from localhost econnreset
Proxy error: Could not proxy request /api/customers from localhost:3000 to localhost:5000/.
See Here for more information (ECONNRESET).
fetch from react
fetch(`${process.env.REACT_APP_API_URL}/customers`)
.then(res => res.json())
.then(res => setData(res))
.catch(err => console.error(err))
.env
REACT_APP_API_URL=/api
package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"dotenv": "^14.2.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000"
}
server.js
const express = require('express');
const cors = require('cors');
const customersAPI = require("./server/customers/customers");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use('/api/customers', customersAPI);
const port = 5000;
app.listen(port, () => `Server running on port ${port}`);
Read more here: Source link