Deployment
Let's deploy the todo app to railway.app.
Prepare for Production
Modify the highlighted code in the src/api.ts
to prefer a connectionString
provided by the production host's DATABASE_URL
environment variable.
// src/api.ts
//...
const DATABASE_URL = process.env["DATABASE_URL"];
export const api = remultExpress({
dataProvider: DATABASE_URL
? createPostgresDataProvider({ connectionString: DATABASE_URL })
: undefined,
//...
})
Note
In order to connect to a local PostgresDB, add DATABASE_URL
to an .env file, or simply replace process.env["DATABASE_URL"]
with your connectionString
.
If no DATABASE_URL
has found, it'll fallback to our local JSON files.
Test Locally
To test the application locally run
npm run build
npm run start
Now navigate to http://localhost:3000 and test the application locally
Deploy to Railway
In order to deploy the todo app to railway you'll need a railway
account. You'll also need Railway CLI installed, and you'll need to login to railway from the cli, using railway login
.
Click enter multiple times to answer all its questions with the default answer
Create a Railway
project
.From the terminal in your project folder run:
shrailway init
Select
Empty Project
Set a project name.
Open the project on
railway
using:shrailway open
Click the
Add Service
and add:- a Postgres Database
- an Empty service
Once that's done run the following command to upload the project to railway:
shrailway up
And select the empty service name that was just created to upload the source code to it.
Once completed, go back to the railway UI in the browser and select the created service (not the database)
Switch to the
variables
tabClick on
+ New Variable
, and in theVARIABLE_NAME
clickAdd Reference
and selectDATABASE_URL
Add another variable called
SESSION_SECRET
and set it to a random string, you can use an online UUID generatorSwitch to the
settings
tabUnder
Environment
click onGenerate Domain
Click the
deploy
button to deploy the changes, and wait for the deployment to completeClick on the newly generated url to open the app in the browser and you'll see the app live in production. (it may take a few minutes to go live)
Note
If you run into trouble deploying the app to Railway, try using Railway's documentation.
That's it - our application is deployed to production, play with it and enjoy.
To see a larger more complex code base, visit our CRM example project
Love Remult? Give our repo a star.⭐