-
-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Description
Hi, I'm unable to set up a proxy for my TypeScript CRA client with my TypeScript Express app. For my CRA, I used:
npx create-react-app client --template typscript
In 'client/src/', I have the typical starter App.tsx file running on localhost:3000
import React from 'react';
import logo from './logo.svg';
import './App.css';
const App: React.FC = () => {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<a href="/auth/google">Sign In With Google</a>
</header>
</div>
);
};
export default App;
Note, the anchor tag:
<a href="/auth/google">Sign In With Google</a>
I am unable to point the href to my express app using the relative route.
My TypeScript CRA is running on localhost:3000, and my TypeScript Express app is running on localhost:5000. After everything I've tried, I still cannot set up a proxy to use relative routes on my CRA to access my TypeScript Express app.
In client/src/ folder, I have a setupProxy.ts file:
(I installed both http-proxy-middleware and @types/http-proxy-middleware dependencies)
import proxy from 'http-proxy-middleware';
export const setupProxy = (app: any) => {
app.use(
proxy(['/api', '/auth/google'], { target: 'http://localhost:5000' })
);
};
In my tsconfig.json file, I tried adding this as well but it didn't work:
"exclude": ["src/setupProxy.js"]
I tried adding this to the client package.json too but it didn't work either.
"proxy": "http://localhost:5000",
The proxy works in a vanilla JS environment, but not so for TypeScript. I apologize if this is a trivial issue, but none of the solutions I've encountered on Google has been working :( I appreciate any help!