setup proxy for vite during developement

This commit is contained in:
Guillaume Dorce 2022-08-21 18:59:36 +02:00
parent 13bba32a8d
commit c8f242705f
3 changed files with 41 additions and 29 deletions

View File

@ -1,34 +1,38 @@
import { useState } from 'react' import { useState } from 'react';
import reactLogo from './assets/react.svg' import './App.css';
import './App.css'
function App() { function App() {
const [count, setCount] = useState(0) const [test, setTest] = useState('Loading...');
const credentials = {
email: 'email',
password: 'password',
};
fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(credentials),
})
.then((res) => res.json())
.then((data) => {
setTest(JSON.stringify(data));
})
.catch((err) => {
setTest(JSON.stringify(err));
});
return ( return (
<div className="App"> <div className="App">
<div>
<a href="https://vitejs.dev" target="_blank">
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a href="https://reactjs.org" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card"> <div className="card">
<button onClick={() => setCount((count) => count + 1)}> <div>
count is {count} <p>test is {test}</p>
</button> </div>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div> </div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</div> </div>
) );
} }
export default App export default App;

View File

@ -1,7 +1,15 @@
import { defineConfig } from 'vite' import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [react()] plugins: [react()],
}) server: {
proxy: {
'/api': {
target: 'http://localhost:5000',
changeOrigin: true,
},
},
},
});

View File

@ -2,7 +2,7 @@ import express, { urlencoded, json } from 'express';
import cors from 'cors'; import cors from 'cors';
import api from '@/api'; import api from '@/api';
const port = process.env.PORT || 3000; const port = process.env.PORT || 5000;
const app = express(); const app = express();
app.use(cors()); app.use(cors());