Skip to content

Commit

Permalink
feat: implements front end proxy using the /api/ route (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelharlow authored Nov 5, 2024
1 parent 9782485 commit dc6b8d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/main/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useCallback, useState} from 'react';
import {useCallback, useEffect, useState} from 'react';
import {Button} from "@/components/ui/button";
import {
Table,
Expand All @@ -17,14 +17,14 @@ function App() {
const navigate = useNavigate();

const getFakeAccounts = useCallback(() => {
fetch("http://localhost:8080/accounts")
fetch("/api/accounts")
.then(response => response.json())
.then(data => setAccounts(data))
.catch(error => console.error(error));
}, [accounts]);

const postFakeAccount = () => {
fetch("http://localhost:8080/account", {
fetch("/api/account", {
method: "POST",
headers: {
"Content-Type": "application/json"
Expand All @@ -42,6 +42,10 @@ function App() {
.catch(error => console.error(error));
};

useEffect(() => {
getFakeAccounts();
}, []);

return (
<div className={"flex flex-col items-center"}>
<h1>Commerce Bank Frontend</h1>
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/src/routes/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Login() {
})

const onSubmit = (data: z.infer<typeof formSchema>) => {
fetch("http://localhost:8080/login", {
fetch("/api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
6 changes: 6 additions & 0 deletions src/main/frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export default defineConfig({
plugins: [react()],
server: {
port: 3000,
proxy: {
"/api": {
target: "http://localhost:8080",
rewrite: (path) => path.replace(/^\/api/, ""), //removes /api prefix from the request path
},
},
},
resolve: {
alias: {
Expand Down

0 comments on commit dc6b8d6

Please sign in to comment.