Instructions on how to implement full fake REST API [json-server] (https://github.com/typicode/json-server) on Heroku hosting site. Should only be used for development purposes, but it can act as a simpler database for smaller applications.
- Press the green
Use this template
button in the right corner. - Give your new repo a name and press the green
Create repository from template
button. - Clone your newly created repository to your computer.
- Change the contents of
db.json
to your own content according to thejson-server example
and thencommit
your changes to git locally.
This example will create /posts
route , each resource will have id
, title
and content
. id
will auto increment!
{
"posts": [
{
"id": 0,
"title": "First post!",
"content": "My first content!"
}
]
}
Heroku is a free hosting service for hosting small projects. Easy setup and deploy from the command line via git.
- Easy setup
- Free
- App has to sleep a couple of hours every day.
- "Powers down" after 30 mins of inactivity. Starts back up when you visit the site but it takes a few extra seconds. Can maybe be solved with Kaffeine
2 . Create an account on
https://heroku.com
3 . Install the Heroku CLI on your computer:
https://devcenter.heroku.com/articles/heroku-cli
4 . Connect the Heroku CLI to your account by writing the following command in your terminal and follow the instructions on the command line:
heroku login
5 . Then create a remote heroku project, kinda like creating a git repository on GitHub. This will create a project on Heroku with a random name. If you want to name your app you have to supply your own name like heroku create project-name
:
heroku create my-cool-project
6 . Update the remote’s details. Replace my-cool-project
with the name of the project:
heroku git:remote -a my-cool-project
7 . Push your app to Heroku (you will see a wall of code):
git push heroku master
8 . Visit your newly create app by opening it via heroku:
heroku open
9 . For debugging if something went wrong:
heroku logs --tail
Heroku will look for a startup-script, this is by default npm start
so make sure you have that in your package.json
(assuming your script is called server.js
):
"scripts": {
"start" : "node server.js"
}
You also have to make changes to the port, you can't hardcode a dev-port. But you can reference herokus port. So the code will have the following:
const port = process.env.PORT || 3000;