Does Railway support ability to set ports?

abibbs
PRO

2 years 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

abibbs
PRO

2 years 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


2 years 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


abibbs
PRO

2 years 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?


2 years 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.


abibbs
PRO

2 years 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.


2 years ago

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


abibbs
PRO

2 years 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.


2 years 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.


2 years 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


abibbs
PRO

2 years ago

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


2 years ago

the default is the start script from your package.json


abibbs
PRO

2 years ago

Lol ok 🤷