a year 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
a year 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
a year 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
a year ago
right I totally get that, I'm just asking why you want to have one domain
a year 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.
a year ago
why not api.netrix.fun
a year ago
It's hard to say, I guess I thought that's how a lot of sites worked
a year ago
I don't really care.
a year ago
not really, like I said, railway has different domains for the frontend and backend
a year ago
perfect, then stick with different domains!
a year ago
have your frontend call the backend domain and call it a day
a year 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)

a year ago
that is correct
a year 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'),
},
}
})a year ago
can you link me to your deployment for the frontend please
a year 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.
a year ago
That's why I opened this thread, to understand how to run both backend and frontend correctly
a year ago
open the deployment and copy the url
a year 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
a year ago
one min
a year ago
https://github.com/netrix-team/frontend I also sent you an invitation. Since this repository is hidden
a year ago
@Brody 👆🏻👆🏻👆🏻
a year ago
alright we are going to do this in steps, trust the process
a year ago
first, remove all the proxy stuff from the config file, just straight delete the proxy stuff
a year ago
I was looking at your template for Vite + React, and was also thinking of using Canddy
a year ago
we will be using that for sure, but one step at a time
a year ago
done

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

a year ago
good enough for me
a year ago
back to frontend
a year ago
well first, does the backend actual use an /api path?
a year ago
wdym thinking lol, you coded it right?
a year 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
a year ago
right but is the backend itself using /api as a route
a year ago
Uh, no
a year ago
without /api, immediately starting with categories, /users, /guilds etc.
a year ago
then whereever you where calling /api replace it with an environmental variable called VITE_BACKEND_URL
a year ago
so?

a year ago
env*
a year ago
Done. Replaced /api wherever it was

a year ago
that works, though i much prefer the + syntax
a year ago
I don't know about that. In fact, I just started learning react
a year ago
"string" + "string"
a year ago
got it
a year ago
Yeah, well, that's an option
a year ago
so how are you calling the discord api from the frontend with auth?
a year 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
a year ago
gotcha, well go ahead and make the necessary code changes for that since you arent using a proxy anymore for that
a year ago
I only use the discord api in two places on the frontend so I just copied the full path
a year ago

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

a year ago
all right

a year ago
oops
a year ago
updated
a year ago
do the fancy save again this time too
a year 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
a year ago
Didn't know about this, it's very helpful. Thank you
a year ago
okay frontend domain is good, push your changes to github
a year ago
and now you just press the button because you do want it to redeploy
a year ago
It's crashing now because there was a mini-flask app running on this service earlier. I've fixed it now
a year ago

a year ago
Everything is working successfully

a year 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

a year ago

a year ago
you are now physically farther way to railways servers than you are with localhost, they are not comparable
a year ago
you just need to choose the railway region closest to you
a year ago
same with the backend, and then the database the backend uses, though preferably you would use a railway hosted database
a year ago
I think I'll survive the delay, as my site was originally intended for American and European audiences
a year 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
a year ago
alright, anything else you need help with?
a year ago
I guess not. Bro, you've been a big help. Thanks for your time
a year ago
no problem!