Hosting json-server on render
Want to run json-server on render?
To start, make sure to have a pre-scripted way (like a run.sh
or npm
script) to run json-server with all the options and flags defined, to point to where your data.json
is, your routes are, etc. This will keep you from needing to log in to render to modify the configuration directly each time your json-server configuration changes in the future.
You can create a new render service by selecting your repo directly:
New > Web Service > Select your repo
When creating your service, make sure to select node as your runtime if it isn’t preselected, and under “advanced” define an environment variable called NODE_VERSION
that matches the node version you’ve been using locally- that’ll make sure the version of node and npm you’re using is consistent across environments.
The free tier is a good use case for json-server. It’s lightweight and idle most of the time so having it spin up and down automatically to save you cost is handy. If you want to prevent it from sleeping, you can use my free keepalive tool to keep it awake and responsive.
Statefulness
One thing to consider about deploying json-server is the statefulness of your data.
Deployed this way, any time the server is restarted (like a wake up from a sleep on the free tier) or redeployed, your data.json
will reset to what it is in your git repo at the commit that deployment is linked to. If you want to preserve the state of your data.json
across deploys, attach a disk to your instance and tell json-server to look in the attached disk for the data.json
.
Disks are persistent storage, so even between restarts and redeploys you won’t lose your data, but they do have a cost: currently, the smallest option is $0.25/GB/month.