2 years ago
If I have two services, for backend and frontend. Frontend is displayed on the domain https://mysite.fun, and backend let's say on some default domain from railway, https://mysite.up.railway.app, but I can somehow link them so that when a request occurs some proxying and the request is sent to https://mysite.fun/api.
I found one video on YouTube where the author realizes this using Docker and NGINX, but he has backend and frontend in one repository : https://github.com/artemonsh/deploy-frontend-backend.
I think this is not a good approach, because if you update one small part of the code on the backend or frontend, the whole application will be redeployed. I want to understand if it is possible to do it with two services or not.
I apologize in advance for possibly stupid questions. I have no experience with Docker or NGINX, so I have no idea how it all works in real projects.
92 Replies
2 years ago
forgive me for asking, but why do you think you need the frontend and backend on different domains?
what's wrong with domain.com as the frontend and api.domain.com as the backend? this in fact is exactly how railway does it
2 years ago
I must have explained it wrong. But I meant that backend and frontend are on different services on Railway. At the same time I would like to have one domain. But that requests are proxied not to the subdomain, but just to /api
2 years ago
right I totally get that, I'm just asking why you want to have one domain
2 years ago
Conditionally:
my site: https:/netrix.fun is where people go.
and to do a search, you have to send a request to https://netrix.fun/api.
2 years ago
why not api.netrix.fun
2 years ago
It's hard to say, I guess I thought that's how a lot of sites worked
2 years ago
I don't really care.
2 years ago
not really, like I said, railway has different domains for the frontend and backend
2 years ago
perfect, then stick with different domains!
2 years ago
have your frontend call the backend domain and call it a day
2 years ago
Sorry, I still don't understand how it works. That is, I need to keep only netrix.fun for the frontend service. And for backend service add api.netrix.fun? (On the screenshot of the frontend service settings)

2 years ago
that is correct
2 years ago
And how will it look like in the react code?
To avoid writing a full url for each request, are there any practices for this? I am currently using this config. So that requests can be organized like this:
axios.get('/api/users/me/guilds')import path from 'path'
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': {
target: 'http://127.0.0.1:8000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
},
'/discord': {
target: 'https://discord.com/api/v10',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/discord/, ''),
secure: false
}
}
},
resolve: {
alias: {
'@constants': path.resolve('./src/constants'),
'@utils': path.resolve('./src/utils'),
'@components': path.resolve('./src/components'),
},
}
})2 years ago
can you link me to your deployment for the frontend please
2 years ago
Where do I get it? I just haven't deployed the full application yet, so far I only have a mini-flask application active that outputs text to the page - it's just on this netrix.fun domain.
2 years ago
That's why I opened this thread, to understand how to run both backend and frontend correctly
2 years ago
open the deployment and copy the url
2 years ago
oh you dont even have it deployed, then please share the repo for the frontend so i can let you know how to make it production ready
2 years ago
one min
2 years ago
https://github.com/netrix-team/frontend I also sent you an invitation. Since this repository is hidden
2 years ago
@Brody 👆🏻👆🏻👆🏻
2 years ago
alright we are going to do this in steps, trust the process
2 years ago
first, remove all the proxy stuff from the config file, just straight delete the proxy stuff
2 years ago
I was looking at your template for Vite + React, and was also thinking of using Canddy
2 years ago
we will be using that for sure, but one step at a time
2 years ago
done

2 years ago
i have yet to see the proxy stuff being useful in production, its only ever in the way of shipping to production
2 years ago
you can remove the server line too
2 years ago
do you have the backend deployed on railway and working?
2 years ago
No, but I can try running it now.
2 years ago
yeah lets do that, your frontend is going to need something to call after all
2 years ago
Okay, I'll try to get it up and running as soon as possible.
2 years ago
let me know if you have any troubles
2 years ago
Great, seems to have successfully launched https://api.netrix.fun/docs
2 years ago
have you tested it?
2 years ago
I made a couple of requests to unsecured endpoints, all successfully returned a response
2 years ago

2 years ago
good enough for me
2 years ago
back to frontend
2 years ago
well first, does the backend actual use an /api path?
2 years ago
wdym thinking lol, you coded it right?
2 years ago
In the Vite configuration, I specified /api just for convenience so I don't have to write the whole url in its entirety. And so the backend is now at api.netrix.fun
2 years ago
right but is the backend itself using /api as a route
2 years ago
Uh, no
2 years ago
without /api, immediately starting with categories, /users, /guilds etc.
2 years ago
then whereever you where calling /api replace it with an environmental variable called VITE_BACKEND_URL
2 years ago
so?

2 years ago
env*
2 years ago
Done. Replaced /api wherever it was

2 years ago
that works, though i much prefer the + syntax
2 years ago
I don't know about that. In fact, I just started learning react
2 years ago
"string" + "string"
2 years ago
got it
2 years ago
Yeah, well, that's an option
2 years ago
so how are you calling the discord api from the frontend with auth?
2 years ago
first user authorizes through discord bot, then special code - which we received from discord - is sent to backend where it is exchanged for access token and then it and all necessary data are entered into jwt token. Which in turn comes to the frontend and is stored in local storage
2 years ago
gotcha, well go ahead and make the necessary code changes for that since you arent using a proxy anymore for that
2 years ago
I only use the discord api in two places on the frontend so I just copied the full path
2 years ago

2 years ago
sounds good
2 years ago
copy the nixpacks.toml and Caddyfile from this repo into yours
2 years ago
done
2 years ago
do you have a service on railway already?
2 years ago
for the frontend that is
2 years ago
yep
2 years ago
link please
2 years ago
2 years ago
I hope I threw the right thing 😅
2 years ago
now set a VITE_BACKEND_URL=https://${{backend.RAILWAY_PUBLIC_DOMAIN}} variable on the frontend service
2 years ago
and to save it, just this once, do alt + shift + enter
2 years ago

2 years ago
all right

2 years ago
oops
2 years ago
updated
2 years ago
do the fancy save again this time too
2 years ago
it saves the change without triggering a deploy, you can do that whenever you are just about to deploy code, no need to make two deployments
2 years ago
Didn't know about this, it's very helpful. Thank you
2 years ago
okay frontend domain is good, push your changes to github
2 years ago
and now you just press the button because you do want it to redeploy
2 years ago
It's crashing now because there was a mini-flask app running on this service earlier. I've fixed it now
2 years ago

2 years ago
Everything is working successfully

2 years ago
But requests take a long time to process for some reason. I thought when I publish the application it will be much faster. I had it running locally for 150-200 milliseconds. I don't know what this has to do with

2 years ago

2 years ago
you are now physically farther way to railways servers than you are with localhost, they are not comparable
2 years ago
you just need to choose the railway region closest to you
2 years ago
same with the backend, and then the database the backend uses, though preferably you would use a railway hosted database
2 years ago
I think I'll survive the delay, as my site was originally intended for American and European audiences
2 years ago
Yeah, that's what I was going to do. The current database is a test database. For production, they'll be located on railway
2 years ago
alright, anything else you need help with?
2 years ago
I guess not. Bro, you've been a big help. Thanks for your time
2 years ago
no problem!