Does Railway support ability to set ports?

abibbsPRO

a year ago

I've built an API that requires knowledge of the base url and port (e.g. http://localhost:3000) so I had to set the port to 3000 using "next start -p 3000" but now my preview builds are showing this error "Application failed to respond".

Any ideas?

Solved

12 Replies

abibbsPRO

a year ago

Solved, for anyone curious it can be done using Procfiles and setting the PORT env variable:
https://docs.railway.app/guides/build-configuration
https://docs.railway.app/guides/public-networking#port-variable


a year ago

Ideally your start script would be next start --port ${PORT-3000}

so that you can use the automatically generated PORT environment variable that Railway expects your app to listen on, or 3000 when running locally. this way there is no need for a Procfile or manually setting a PORT service variable


Status changed to Solved railway[bot] over 1 year ago


abibbsPRO

a year ago

I noticed that when building, Nixpacks didn't recognize the port parameter. Adding it to the Procfile resolved this however. Am I perhaps misunderstanding the purpose of the Procfile?


a year ago

The Procfile sets the start command, but typically for a node app the start command is defined by the package.json's start script.

the PORT parameter is also not used during build, so not too sure what's going on there, Railway's docs site is next and uses the start script I mentioned above without issue.


abibbsPRO

a year ago

In other words I had to add next start -p 3000 to my package.json, but Nixpacks still only ran next start so I had to also create a Procfile and add web: next start -p 3000.


a year ago

That means you likely had the old start command also set somewhere else, there are numerous places you can set the start command.


abibbsPRO

a year ago

Interesting but no, it's only defined in my package.json. I ran a search of my entire project to double check as well.


a year ago

I'm sorry but that's how railway works, the start script is used as the start command unless you have it overwritten somewhere else.


a year ago

https://github.com/railwayapp/docs/blob/main/package.json#L9

there is also no Procfile, that start script is all you need provided the start command is not set somewhere else


abibbsPRO

a year ago

So what you're saying is that Railway doesn't define default commands for Nixpacks?


a year ago

the default is the start script from your package.json


abibbsPRO

a year ago

Lol ok 🤷