You've finished building a Node.js app and pushed it to GitHub, but how do you host it on Railway?
-
In app.js of your project, add '0.0.0.0' to the app.listen function as shown below. Railway requires your app to explicitly listen on all network interfaces (0.0.0.0) instead of just the default localhost. Push this change to GitHub.
-
From the Railway dashboard, selectNewandDeploy from GitHub repo.Select your project, clickDeploy Now,and wait for deployment to complete.
-
Close the side window and click on theCreatebutton. SelectDatabaseand thenAdd PostgreSQL.Wait for deployment to complete.
-
With the project card still selected, navigate to theVariablestab. Click onNew Variableand add the Railway-providedDATABASE_URL.You should be able to start typing it in VARIABLE_NAME and select it from the dropdown menu. Railway will populate the value for you, then clickAdd.
-
Click on thePostgrescard on the left and navigate to theVariablestab. You should see all the Railway-provided Postgres variables. Copy theDATABASE_PUBLIC_URL(if you want your app to be public).
-
In your terminal, change directory to your project. Run the following commands:
export DATABASE_URL=<DATABASE_PUBLIC_URL>
Run your script to populate the database.
node db/populatedb.js <DATABASE_PUBLIC_URL>
-
Great! You should see your table(s) in the PostgresDatatab.
-
Go to projectSettingsand scroll down toNetworking.UnderPublic Networking,click onGenerate Domain.
That's it! Click on the public domain, and you should be able to see your project working.
Hope this tutorial helps!