502 on app with correct HOST and PORT

theo-nejmHOBBY

10 months ago

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?

0 Replies

theo-nejmHOBBY

10 months ago

75f448f4-88c9-4d82-b428-61740d90e40c


theo-nejmHOBBY

10 months ago

My public route is this:
niki-backend-homolog.up.railway.app


theo-nejmHOBBY

10 months ago

It just works if I specify the app port to 3000 on railway env :/


10 months ago

hey, send your app.listen.
I also use nest and have no problems, pretty weird.


theo-nejmHOBBY

10 months ago

await app.listen(process.env.PORT || 3000, '0.0.0.0');


10 months 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.

1278189202911137800


theo-nejmHOBBY

10 months ago

k, imma try removing the 0.0.0.0


theo-nejmHOBBY

10 months ago

but it was not working also


theo-nejmHOBBY

10 months ago

still not working


theo-nejmHOBBY

10 months ago

when i removed the 0.0.0.0 and port


theo-nejmHOBBY

10 months ago

just works when i specify the port :/


10 months ago

weird, are u using fastify or express?


theo-nejmHOBBY

10 months ago

fastify


10 months ago

hmmm im using express so it might have a difference


theo-nejmHOBBY

10 months ago

hmm maybe


10 months 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-nejmHOBBY

10 months ago


theo-nejmHOBBY

10 months ago

without solution


theo-nejmHOBBY

10 months ago

(when I specify the port it works)


theo-nejmHOBBY

10 months ago

should I specify this and just move forward?


theo-nejmHOBBY

10 months ago

should I have any problem by doing this?


10 months ago

couldnt find any other issue related to fastify and 502


10 months ago

yep its alright


theo-nejmHOBBY

10 months ago

oh, i have another problem


theo-nejmHOBBY

10 months ago

the database cannot connect to run migrations without the public url


theo-nejmHOBBY

10 months ago

my build command runs the migrations before the build


theo-nejmHOBBY

10 months ago

to keep the db up-to-date


theo-nejmHOBBY

10 months ago

but it just works when I add an env variable with the public database url


10 months ago

the private networking isn't available at build unfortunately.
in my case I do the migration on the start script


theo-nejmHOBBY

10 months ago

hmm


theo-nejmHOBBY

10 months ago

is there any problem to use the public url?


theo-nejmHOBBY

10 months ago

(just for migrations)


10 months 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-nejmHOBBY

10 months ago

hmm, it is used only at migrations, so I guess there is no problem


10 months ago

If you're sure of that then ok


10 months ago

but tbh I would just move the migration script to the start command


theo-nejmHOBBY

10 months ago

you put migrations before start, right?


theo-nejmHOBBY

10 months ago

with && operator


10 months ago

yep, are u using prisma?


theo-nejmHOBBY

10 months ago

if migrations fail, you dont start?


theo-nejmHOBBY

10 months ago

typeorm


10 months ago

ah typeorm


theo-nejmHOBBY

10 months ago

okay, imma try this approach


theo-nejmHOBBY

10 months ago

put it on start script


10 months ago

yep but my backend contains a healthcheck
if yours dont then it would just fail



theo-nejmHOBBY

10 months ago

lemme understand
in this case, what does the healthcheck change?


10 months ago

this is my script with prisma:

"start": "prisma generate --no-hints && prisma migrate deploy && node dist/src/main",

theo-nejmHOBBY

10 months ago

i didnt set a healthcheck yet


10 months ago

Railway will hold the deployment of your application until its actually reachable by doing an HTTP ping to a /health endpoint


10 months ago

you've to configure it in the service settings


theo-nejmHOBBY

10 months ago

oh, so it will not stop trying to restart until the app is up?


10 months ago

trying to restart? you mean an infinite loop of fail restart?


theo-nejmHOBBY

10 months ago

yup


10 months ago

nope, thats another setting

1278193155992195000


theo-nejmHOBBY

10 months ago

where should I configure health route?


10 months ago

1278193469931917300


10 months ago

go to your service settings and scroll down until you see that


theo-nejmHOBBY

10 months ago

oh good


theo-nejmHOBBY

10 months ago

will this be enough?

@Controller('health')
export class HealthController {
  @Get()
  healthCheck() {
    return { ok: true };
  }
}

10 months ago

yep it should be alright


10 months ago

nestjs also offers the option to check your database and other external services but thats optional, that health controller will work


theo-nejmHOBBY

10 months ago

imma see them later, if my db is down, the routes will be the smaller problem lmao


10 months ago

makes sense <:kekw:788259314607325204>


theo-nejmHOBBY

10 months ago

ive added the route, but its not working, probably because previous deploy was not with port


10 months ago

wdym by not working? 502 or build not going through?


theo-nejmHOBBY

10 months ago

502


theo-nejmHOBBY

10 months ago

the deploy with the port is not working


theo-nejmHOBBY

10 months ago

because the previous one was the health one


theo-nejmHOBBY

10 months ago

and the status is 502, because there is no port yet


theo-nejmHOBBY

10 months ago

🤣


theo-nejmHOBBY

10 months ago

removed healthcheck for a while


10 months ago

no port yet? Railway should just do a load balance so both of the deploys will contain the port


theo-nejmHOBBY

10 months ago

the problem was the 0.0.0.0 probably


theo-nejmHOBBY

10 months ago

it appears to be needed on fastify apps


10 months ago

yep, i'll see if I can get my fastify app to see what I did but it just worked 💀


theo-nejmHOBBY

10 months ago

gods with u


10 months ago

yep it just works 💀

1278197666362294300


theo-nejmHOBBY

10 months ago

not using process to env? 😕


10 months ago

I use to validate my env variables and then import it elsewhere


theo-nejmHOBBY

10 months ago

interesting


theo-nejmHOBBY

10 months ago

u use redis in any of your nest apps?


theo-nejmHOBBY

10 months ago

deployed on railway


10 months ago

yes


theo-nejmHOBBY

10 months ago

does it worth it?


10 months ago

you mean in terms of pricing?


theo-nejmHOBBY

10 months ago

yes


theo-nejmHOBBY

10 months ago

instead of using the instance memory itself


10 months ago

well Railway's memory pricing is not that cheap but if your just using for cache it should be alright


theo-nejmHOBBY

10 months ago

well, health check is now working


theo-nejmHOBBY

10 months ago

obrigado amigo


theo-nejmHOBBY

10 months ago

late night, imma sleep


10 months ago

I used to host a bullmq queue with at least 16k per minute inserts and it was pretty cheap IMO


10 months ago

great!


10 months ago

boa noite!