From 40e178874e129545c7de06f9f63a8ba15b93d1a2 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Sun, 9 Feb 2025 01:37:14 -0500 Subject: set up flask proxy --- api/api.py | 2 +- package.json | 3 +-- src/App.jsx | 45 ++++++++++++++++----------------------------- vite.config.js | 7 ++++++- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/api/api.py b/api/api.py index 3e16e58..64d9fe5 100644 --- a/api/api.py +++ b/api/api.py @@ -3,6 +3,6 @@ from flask import Flask app = Flask(__name__) -@app.route('/time') +@app.route('/api/time') def get_current_time(): return {'time': time.time()} diff --git a/package.json b/package.json index ae544fc..2620335 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,5 @@ "eslint-plugin-react-refresh": "^0.4.18", "globals": "^15.14.0", "vite": "^6.1.0" - }, - "proxy": "http://localhost:5000" + } } diff --git a/src/App.jsx b/src/App.jsx index f67355a..2f196f9 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,35 +1,22 @@ -import { useState } from 'react' -import reactLogo from './assets/react.svg' -import viteLogo from '/vite.svg' +import { useEffect, useState } from 'react' import './App.css' function App() { - const [count, setCount] = useState(0) + const [currentTime, setCurrentTime] = useState(0); - return ( - <> -
- - Vite logo - - - React logo - -
-

Vite + React

-
- -

- Edit src/App.jsx and save to test HMR -

-
-

- Click on the Vite and React logos to learn more -

- - ) + useEffect(() => { + fetch('/api/time').then(res => res.json()).then(data => { + setCurrentTime(data.time); + }); + }, []); + + return ( +
+
+

The current time is {currentTime}.

+
+
+ ) } -export default App +export default App; diff --git a/vite.config.js b/vite.config.js index 8b0f57b..df070a7 100644 --- a/vite.config.js +++ b/vite.config.js @@ -3,5 +3,10 @@ import react from '@vitejs/plugin-react' // https://vite.dev/config/ export default defineConfig({ - plugins: [react()], + plugins: [react()], + server: { + proxy: { + '/api': 'http://localhost:5000', + }, + }, }) -- cgit v1.2.3