JulianGaramendy.dev
Persistent REST API with json-server and Glitch
31 August, 2020This is the easiest way I know to get a public persistent REST API up and running in under 1 minute, without writing any code.
We'll be using json-server by tipicode hosted on Glitch.
Step 1: Clone/Remix the demo project
Head over to Glitch.com and "remix" my json-server-demo.
Step 2: Use your own data
You can change db.json with your own json "database".
The one in the demo looks like this:
{
"games": [
{
"id": 1,
"title": "Frogger",
"year": 1981
...
},
...
]
}
Step 3: That's it!
While on the Glitch project, click on "Show in a new window", and you'll see the URL/endpoint of your REST API.
In the demo's db.json file, "games"
becomes an entity that you can access like this:
GET https://json-server-demo.glitch.me/games
POST https://json-server-demo.glitch.me/games
PATCH https://json-server-demo.glitch.me/games/1
DELETE https://json-server-demo.glitch.me/games/1
How it works
Glitch projects can run Node.js, but in our case we don't need to write a single line of code. We simply declare our dependency to json-server and our "start" script in the package.json file:
{
...
"scripts": {
"start": "json-server --watch ./db.json"
},
"dependencies": {
"json-server": "^0.16.1"
},
...
}
By default json-server reads and writes to the db.json file, so all changes made by POST, PATCH, PUT, DELETE http methods are persisted in "disk" in the Glitch project. See Do you have built-in persistence or a database? in the FAQ.
The answer is YES!
This means you can: Use files as a flat file database
Warning: Glitch Restrictions
Glitch "projects" seem to take some time to warm up or wake up, and the go back to sleep after a period of inactivity. For this reason I think this quick setup is good for demos or workshops, but not for production.
Please refer to the links below for more information:
Photo by coniferconifer on Wikimedia Commons
Comment on dev.to: https://dev.to/juliang/persistent-rest-api-with-json-server-and-glitch-32kc