R
Railwayā€¢3mo ago
theo

502 on app with correct HOST and PORT

Hi, friends! I'm deploying a new app on railway, I've followed the guides without problem. The build was successful, the application is running, but I can't access this, because I'm getting a 502 status code. https://docs.railway.app/guides/fixing-common-errors I've followed this guide but didn't solved my problem. I'm already using the PORT that railway provides and the host is set to 0.0.0.0 Am I missing something?
Railway Docs
Fixing Common Errors | Railway Docs
Documentation for Railway
59 Replies
Percy
Percyā€¢3mo ago
Project ID: 75f448f4-88c9-4d82-b428-61740d90e40c
theo
theoOPā€¢3mo ago
75f448f4-88c9-4d82-b428-61740d90e40c My public route is this: niki-backend-homolog.up.railway.app It just works if I specify the app port to 3000 on railway env :/
ThallesComH
ThallesComHā€¢3mo ago
hey, send your app.listen. I also use nest and have no problems, pretty weird.
theo
theoOPā€¢3mo ago
await app.listen(process.env.PORT || 3000, '0.0.0.0');
await app.listen(process.env.PORT || 3000, '0.0.0.0');
ThallesComH
ThallesComHā€¢3mo ago
I don't even need the 0.0.0.0, maybe you could give it a try? but anyway, the port env would work without any problems.
No description
theo
theoOPā€¢3mo ago
k, imma try removing the 0.0.0.0 but it was not working also still not working when i removed the 0.0.0.0 and port just works when i specify the port :/
ThallesComH
ThallesComHā€¢3mo ago
weird, are u using fastify or express?
theo
theoOPā€¢3mo ago
fastify
ThallesComH
ThallesComHā€¢3mo ago
hmmm im using express so it might have a difference
theo
theoOPā€¢3mo ago
hmm maybe
ThallesComH
ThallesComHā€¢3mo ago
I guess I've an application where I used fastify but anyway the port env should be working alright so I guess there's no need to do something about it
theo
theoOPā€¢3mo ago
i see a question here with the same problem, probably? https://help.railway.app/questions/my-backend-is-launched-without-probleme-9970071f without solution (when I specify the port it works) should I specify this and just move forward? should I have any problem by doing this?
ThallesComH
ThallesComHā€¢3mo ago
couldnt find any other issue related to fastify and 502 yep its alright
theo
theoOPā€¢3mo ago
oh, i have another problem the database cannot connect to run migrations without the public url my build command runs the migrations before the build to keep the db up-to-date but it just works when I add an env variable with the public database url
ThallesComH
ThallesComHā€¢3mo ago
the private networking isn't available at build unfortunately. in my case I do the migration on the start script
theo
theoOPā€¢3mo ago
hmm is there any problem to use the public url? (just for migrations)
ThallesComH
ThallesComHā€¢3mo ago
by using public url you'll be charged more by bandwidth. I don't know how much you would but I'm pretty sure it would be cheap
theo
theoOPā€¢3mo ago
hmm, it is used only at migrations, so I guess there is no problem
ThallesComH
ThallesComHā€¢3mo ago
If you're sure of that then ok but tbh I would just move the migration script to the start command
theo
theoOPā€¢3mo ago
you put migrations before start, right? with && operator
ThallesComH
ThallesComHā€¢3mo ago
yep, are u using prisma?
theo
theoOPā€¢3mo ago
if migrations fail, you dont start? typeorm
ThallesComH
ThallesComHā€¢3mo ago
ah typeorm
theo
theoOPā€¢3mo ago
okay, imma try this approach put it on start script
ThallesComH
ThallesComHā€¢3mo ago
yep but my backend contains a healthcheck if yours dont then it would just fail
ThallesComH
ThallesComHā€¢3mo ago
Documentation | NestJS - A progressive Node.js framework
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
theo
theoOPā€¢3mo ago
lemme understand in this case, what does the healthcheck change?
ThallesComH
ThallesComHā€¢3mo ago
this is my script with prisma:
"start": "prisma generate --no-hints && prisma migrate deploy && node dist/src/main",
"start": "prisma generate --no-hints && prisma migrate deploy && node dist/src/main",
theo
theoOPā€¢3mo ago
i didnt set a healthcheck yet
ThallesComH
ThallesComHā€¢3mo ago
Railway will hold the deployment of your application until its actually reachable by doing an HTTP ping to a /health endpoint you've to configure it in the service settings
theo
theoOPā€¢3mo ago
oh, so it will not stop trying to restart until the app is up?
ThallesComH
ThallesComHā€¢3mo ago
trying to restart? you mean an infinite loop of fail restart?
theo
theoOPā€¢3mo ago
yup
ThallesComH
ThallesComHā€¢3mo ago
nope, thats another setting
No description
theo
theoOPā€¢3mo ago
where should I configure health route?
ThallesComH
ThallesComHā€¢3mo ago
No description
ThallesComH
ThallesComHā€¢3mo ago
go to your service settings and scroll down until you see that
theo
theoOPā€¢3mo ago
oh good will this be enough?
@Controller('health')
export class HealthController {
@Get()
healthCheck() {
return { ok: true };
}
}
@Controller('health')
export class HealthController {
@Get()
healthCheck() {
return { ok: true };
}
}
ThallesComH
ThallesComHā€¢3mo ago
yep it should be alright nestjs also offers the option to check your database and other external services but thats optional, that health controller will work https://docs.nestjs.com/recipes/terminus#setting-up-a-healthcheck
theo
theoOPā€¢3mo ago
imma see them later, if my db is down, the routes will be the smaller problem lmao
ThallesComH
ThallesComHā€¢3mo ago
makes sense :kekw:
theo
theoOPā€¢3mo ago
ive added the route, but its not working, probably because previous deploy was not with port
ThallesComH
ThallesComHā€¢3mo ago
wdym by not working? 502 or build not going through?
theo
theoOPā€¢3mo ago
502 the deploy with the port is not working because the previous one was the health one and the status is 502, because there is no port yet šŸ¤£ removed healthcheck for a while
ThallesComH
ThallesComHā€¢3mo ago
no port yet? Railway should just do a load balance so both of the deploys will contain the port
theo
theoOPā€¢3mo ago
the problem was the 0.0.0.0 probably it appears to be needed on fastify apps
ThallesComH
ThallesComHā€¢3mo ago
yep, i'll see if I can get my fastify app to see what I did but it just worked šŸ’€
theo
theoOPā€¢3mo ago
gods with u
ThallesComH
ThallesComHā€¢3mo ago
yep it just works šŸ’€
No description
theo
theoOPā€¢3mo ago
not using process to env? šŸ˜•
ThallesComH
ThallesComHā€¢3mo ago
I use https://github.com/t3-oss/t3-env to validate my env variables and then import it elsewhere
theo
theoOPā€¢3mo ago
interesting u use redis in any of your nest apps? deployed on railway
ThallesComH
ThallesComHā€¢3mo ago
yes
theo
theoOPā€¢3mo ago
does it worth it?
ThallesComH
ThallesComHā€¢3mo ago
you mean in terms of pricing?
theo
theoOPā€¢3mo ago
yes instead of using the instance memory itself
ThallesComH
ThallesComHā€¢3mo ago
well Railway's memory pricing is not that cheap but if your just using for cache it should be alright
theo
theoOPā€¢3mo ago
well, health check is now working obrigado amigo late night, imma sleep
ThallesComH
ThallesComHā€¢3mo ago
I used to host a bullmq queue with at least 16k per minute inserts and it was pretty cheap IMO great! boa noite!
Want results from more Discord servers?
Add your server