BE/FE Communication

Hey. I have a project setup with BE(DRF) and my FE(React Vite). I managed to get them both to build and run separately, but for I can't seem to get them to communicate with each other. FE is running on port 80, and be on port 8000. Running locally works. What I've tried so far: I've added the PORT variable and set it to 8000 on my DRF and 80 on my FE. I've updated the env variables on my FE to instead point to the internal url to my backend: VITE_BACKEND_URL=https://backend.railway.internal:8000 BE logs says absolutely nothing, stays quiet. No deploy logs updating. When communicating between two services within the same project, am I not supposed to be able to use this internal url? Am I misunderstanding something here? First time using Railway. I'm sure my description is ver vague for troubleshooting this so just let me know whatever you need. It's a private repo rn, but can invite to it.
Solution:
and for potentially helping future developers with similar/same issue. TLDR: 1. React Vite is client side, so you will need to use a public network url to access your backend when doing API calls....
Jump to solution
48 Replies
Percy
Percy•13mo ago
Project ID: a10da84f-4e0c-4d54-a745-240ce1a24b59
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
a10da84f-4e0c-4d54-a745-240ce1a24b59
Lelebees
Lelebees•13mo ago
You're unable to connect over both internet and private networking, correct?
Brody
Brody•13mo ago
going forward can I ask that you use the full word, frontend and backend, I had to read this like 3 times to understand what you meant. and I don't think I fully understand it still, are you trying to call the backend from the frontend with the private domain?
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Absolutely. Correct. I'm trying to make API calls from my frontend to the backend. For local development I previously used localhost:3001 for my frontend, and localhost:8000 for my backend. I then updated my frontend environment to instead use backend.railway.internal:8000 as the base for any API calls instead of localhost:8000, but the request times out and no events are logged at all in my backend deployment logs. I've attached my deployment logs, env variables and backend networking settings if its any help.
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
No description
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Messed up the images format. sec.
Brody
Brody•13mo ago
you said you have a react app done with vite
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
correct
Brody
Brody•13mo ago
that means it's a client side rendered app, aka the clients browser makes the api call, for this your frontend would need to use the public backend domain. wouldn't be much of a private network if anyone's browser could use those private domains
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
No description
No description
Brody
Brody•13mo ago
and please don't run django in development mode, use gunicorn
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Alright. I'll update it to the backends public url then and get back to you
Brody
Brody•13mo ago
this too!
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Is that a requirement for it to run/work at all?
Brody
Brody•13mo ago
running a development server is more resource demanding and far less stable, it's just not something you should do
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Ah yeah. I'm just testing the platform out right now. This will not be the final deployment. Speaking of that though - are there any templates for using django with gunicorn available for railway? Iv'e seen that you've helped share a couple of other setups previously for other environments
Brody
Brody•13mo ago
https://railway.app/template/GB6Eki but you would specifically want this https://github.com/railwayapp-templates/django/blob/main/railway.json of course change mysite for whatever is applicable to you
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
That's great. Thank you for that. I'm currently redeploying with the public url ok, no luck with the public network url. same issue as previously, any API requests sent to the backend.
Brody
Brody•13mo ago
send the public url please
Brody
Brody•13mo ago
running your app with gunicorn will fix this
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Alright, on it.
Brody
Brody•13mo ago
you could get it to work with the dev server, but im not gonna tell you how to do something the wrong way 🤣
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
haha, fair Alright. The public network url works now. Sent a request from my frontend but still dead silent from the backend - no activity there. Not sure what I am forgetting. I'll compare with the django template settings to see if I missed something.
Brody
Brody•13mo ago
what response do you get when you make api calls to the backend?
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
It's basically just an error response. Going to improve the error handling to see if I can get it to show more accurately. Right now its a net::ERROR
Brody
Brody•13mo ago
hey at least its not a 503
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Very true! At least it is alive. I still feel like there must be some setting I've missed, just can't figure it out. PORT is set to 8000 on the backend with variables. Do I not specify the port when sending requests anymore? Like the url right now is https://backend-production-1cf6.up.railway.app/ should I exclude the :8000?
Brody
Brody•13mo ago
yes, you can only access the app externally from port 443
Brody
Brody•13mo ago
and the error is pretty self explanatory
No description
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Yeah, will take care of that too. Just want to see them 'communicate' even if the backend rejects it
Brody
Brody•13mo ago
if you want your backend to print access logs you need to enable that with gunicorn https://docs.gunicorn.org/en/stable/settings.html#logging add --access-logfile - to your start command
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Right. And the 443, I don't need to include that either right, I'll just exclude the port all together? in my api requests from the frontend
Brody
Brody•13mo ago
correct, you are using https so port 443 is implied
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Great. The "PORT" env variable that I set to 8000 would not impact this right? on the backend.
Brody
Brody•13mo ago
gunicorn listens on PORT by default, so you could actually remove it since railway will set a PORT if you dont
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Alright. Removed the port, added allowed hosts and logging for gunicorn. still no response to my actions on the frontend, gmm hmm
Brody
Brody•13mo ago
what error do you get on the frontend?
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Ok, it's not a railway issue. I tested from Thunderclient now and just sent a random API request and it does respond.
Brody
Brody•13mo ago
Ok, it's not a railway issue.
haha i could have told you that 😆
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
I mean, it's not a misconfiguration in Railway on my end hehe. I was thinking I might have misunderstood it. Somehow the google login fails, or rather it fails half of the authentication after receiving the authentication code. not sure what differs between this and my local development though, since it works there. Wooo, communication! One more re-deploy now after fixing some CORS-issues and we might be good! (except all the other 1k errors and security settings) lol.
Brody
Brody•13mo ago
and dont run in debug when you have it working lol
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Oh yeah, something I'd like to ask. Since I now have a public network url for my backend, how do I keep that inaccessible for other users in the future? I obviously don't want people to be able to open the admin panel for example, or even try and open the url.
Brody
Brody•13mo ago
passwords and api keys
Solution
Smack | DM FOR NODE RENT
and for potentially helping future developers with similar/same issue. TLDR: 1. React Vite is client side, so you will need to use a public network url to access your backend when doing API calls. 2. Set up django with gunicorn 3. you dont necessarily need the "PORT" variable set on the backend, since gunicorn handles it. And finally my resolution for the 'communication' between them was that I hadn't added the backend public url as an authorized redirect url in the Google Console. So my authentication stopped half way.
Smack | DM FOR NODE RENT
Smack | DM FOR NODE RENTOP•13mo ago
Thanks for all the help @Brody 🙂
Brody
Brody•13mo ago
no problem! 🙂
Want results from more Discord servers?
Add your server