R
Railway10mo ago
IssaKass

Flask + React deployment

I have a flask + react app that is running locally. How would I deploy it to Railway? I tried to have 2 services, one for the frontend and the other for the backend, but the frontend was unable to fetch data from the backend returning with: unexpected token < JSON. The backend is working and I tested it via postman. Projet id: c6894929-c040-4842-8c83-4516d57a14fe
55 Replies
Brody
Brody10mo ago
I tried to have 2 services, one for frontend the other for backend
that is the correct way to go about it, so lets dig into why it wasnt working can you share your repo? it would help tremendously
Brody
Brody10mo ago
would you be okay adding me?
IssaKass
IssaKass10mo ago
Yes
Brody
Brody10mo ago
brody192
IssaKass
IssaKass10mo ago
How to add you?
Brody
Brody10mo ago
haha i dont know, ive never added anyone to a private repo, i dont even think i have a private repo though thats not something a quick google search cant solve!
IssaKass
IssaKass10mo ago
I will change them to public
Brody
Brody10mo ago
that works too
IssaKass
IssaKass10mo ago
I changed them
Brody
Brody10mo ago
okay backend looks good may i ask if you followed any tutorials for the frontend stuff?
IssaKass
IssaKass10mo ago
Yes, I have followed a react course in scrimba and made many frontend projects
Brody
Brody10mo ago
link me it please
IssaKass
IssaKass10mo ago
Scrimba
React Tutorial: Learn React JS - Free 11-Hour Course
The ultimate React 101 - the perfect starting point for any React beginner. Learn the basics of modern React by solving 140+ interactive coding challenges and building eight fun projects.
Brody
Brody10mo ago
the proxy stuff in the vite.config.js file, does that guide tell you to do that?
IssaKass
IssaKass10mo ago
No, but have seen it in another place, couldn't remember In development mode it works, and when I build and preview, also works Also in deployment, when fetching data, the response is Ok but getting the console error And the data is not displayed
Brody
Brody10mo ago
well I'll say that's is not a good way to do it, I have seen someone else very recently do it the exact same way and they had issues too can you show me a screenshot of your railway project?
IssaKass
IssaKass10mo ago
No description
IssaKass
IssaKass10mo ago
Try to fetch the data, and see the result from Network tab
Brody
Brody10mo ago
no sorry I meant the railway project, on the railway website, not your project 🤣
IssaKass
IssaKass10mo ago
Ok
IssaKass
IssaKass10mo ago
No description
Brody
Brody10mo ago
please remove the brackets from the service names, they will cause issues down the line
IssaKass
IssaKass10mo ago
Done this task
Brody
Brody10mo ago
new screenshot please
IssaKass
IssaKass10mo ago
No description
Brody
Brody10mo ago
perfect
IssaKass
IssaKass10mo ago
I tried to fetch data and worked
Brody
Brody10mo ago
we want to get rid of that proxy stuff though, so let's do that now
IssaKass
IssaKass10mo ago
Ok
Brody
Brody10mo ago
so remove the proxy stuff from the config file
IssaKass
IssaKass10mo ago
I will open the laptop, a minute please Ok But removing proxy won't work for development mode
Brody
Brody10mo ago
on the frontend railway service add a service variable VITE_API_URL=https://${{Backend.RAILWAY_PUBLIC_DOMAIN}} this will dynamically render as the backend's domain. in code you would now want to use that variable, so here https://github.com/IssaKass/financial-app-frontend/blob/main/src/App.jsx#L9 the fetch call would use import.meta.env.VITE_API_URL
IssaKass
IssaKass10mo ago
In dev mode, I am accessing the local flask servwr
Brody
Brody10mo ago
you dont need a proxy for local development work i dont even know why thats a feature
IssaKass
IssaKass10mo ago
The proxy is set to access flask via /API, so if you have a flask endpoint to /projects, you can access it via /API/projects
Brody
Brody10mo ago
but thats simply not needed, instead you would do import.meta.env.VITE_API_URL + /projects/ in the fetch call
IssaKass
IssaKass10mo ago
This would work?
Brody
Brody10mo ago
yes, why wouldn’t it, locally that VITE_API_URL variable is read from this file https://github.com/IssaKass/financial-app-frontend/blob/main/.env.development#L2
IssaKass
IssaKass10mo ago
Can you show me the final vite config file?
IssaKass
IssaKass10mo ago
And I have .env.development and .env.production, should I remove the second one?
Brody
Brody10mo ago
yeah you can remove https://github.com/IssaKass/financial-app-frontend/blob/main/.env.production since your variables for production are set in the railway service variables tab
IssaKass
IssaKass10mo ago
THANK YOU VERY MUCH 😊
Brody
Brody10mo ago
all working now?
IssaKass
IssaKass10mo ago
Yes
Brody
Brody10mo ago
awsome!
IssaKass
IssaKass10mo ago
Can I ask another question ❓
Brody
Brody10mo ago
of course
IssaKass
IssaKass10mo ago
How to improve the backend response time It takes 250ms, is it a good responding time, or it is too long
Brody
Brody10mo ago
thats not bad at all, where abouts in the world are you located, since the railway deployment is made in us-west
IssaKass
IssaKass10mo ago
I am from Lebanon, is it better to change the deployment location?
Brody
Brody10mo ago
changing the deployment location is a pro feature, but 250ms is pretty fine for a hobby site in my opinion
IssaKass
IssaKass10mo ago
Thanks
Brody
Brody10mo ago
no problem!
Want results from more Discord servers?
Add your server