I'm trying to connect node.js with react.js but proxy is not working
this is server.js server starts on localhost:5000
const express = require('express');
const server = express();
const cors = require('cors');
server.use(cors())
server.get('/api',(req,res)=>{
res.json({"server":["ready","link start"]})
})
server.listen(process.env.PORT || 5000,()=>{
console.log('link start.....!!!');
})
this is package.json in react and i don't know why the proxy does not work
{
"name": "clone",
"devServer": {
"inline": true,
"contentBase": "./dist",
"proxy": {
"/api/**": {
"target": "http://localhost:5000/",
"secure": false
}
}
},
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"express": "^4.18.1",
"nodemon": "^2.0.18",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
"main": "index.js",
"keywords": [],
"author": "",
"license": "ISC"
}
this is App.js App.js react
import logo from './logo.svg';
import './App.css';
import React,{useEffect,useState} from 'react';
function App() {
const [backend,setbackend]=useState([{}]);
useEffect(()=>{
fetch('/api').then(
response => response.json()
).then(
data=>{
setbackend(data)
}
)
},[])
return (
<div></div>
);
}
export default App;
I want to start react on localhost:5000
- how should I do it
- I don't know what is wrong