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, select New and Deploy from GitHub repo. Select your project, click Deploy Now, and wait for deployment to complete.
-
Close the side window and click on the Create button. Select Database and then Add PostgreSQL. Wait for deployment to complete.
-
With the project card still selected, navigate to the Variables tab. Click on New Variable and add the Railway-provided DATABASE_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 click Add.
-
Click on the Postgres card on the left and navigate to the Variables tab. You should see all the Railway-provided Postgres variables. Copy the DATABASE_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 Postgres Data tab.
-
Go to project Settings and scroll down to Networking. Under Public Networking, click on Generate Domain.
That's it! Click on the public domain, and you should be able to see your project working.
Hope this tutorial helps!
Top comments (0)