Dummy API
- Press
Use this template
button in the right corner - Grive your new repository a name and press
Create repository from template
button - Clone your newly created repository to your computer
- Change the contents of
db.json
androutes.json
(optional) to your own content according to the json-server example and then commit your changes to git locally.
- This example will create
/users
route, each resource will haveid
,name
andemail
// db.json
{
"users": [
{
"id": 1,
"name": "Ngoc Khuong",
"email": "me@ngockhuong.com"
}
]
}
- This example will define a custom route, search user by email
// routes.json
{
"/users/email/:email": "/users?email=:email"
}
- If you want to change home page template, please update the contents at
public/
The recommended way to run this container looks like this:
$ DB_FILE=db.json && ROUTES_FILE=routes.json && docker run -d \
--name dummy-api \
-p 3000:3000 \
-v $(pwd)/$DB_FILE:/app/db.json \
-v $(pwd)/$ROUTES_FILE:/app/routes.json \
lamngockhuong/dummy-api:latest
The above example exposes the JSON Server REST API on port 3000, so that you can now browse to: http://localhost:3000
This is a rather common setup following docker's conventions:
-d
will run a detached instance in the background-p {OutsidePort}:3000
will bind the webserver to the given outside port-v {AbsolutePathToDBJsonFile}:/app/db.json
should be passed to mount the given DB JSON file into the container-v {AbsolutePathToRoutesJsonFile}:/app/routes.json
should be passed to mount the given Routes JSON file into the containerlamngockhuong/dummy-api
the name of this docker image
When you update db.json or routes.json, please restart dummy-api container:
$ docker restart dummy-api
Heroku is a free hosting service for small projects. Easy setup and deploy from the command line via git.
Pros:
- Easy setup
- Free
Cons:
- 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
- Create your database
- Create an account on https://heroku.com
- Login and Create new app
- Go to Deploy tab > Deployment method > Choose GitHub
- Go to Connect to GitHub, connect to your github account and search your repository > Click Connect
- Go to Manual deploy > Choose a branch to deploy > Press Deploy Branch
- Go to Overview tab to view build log and more.
-
Install the Heroku CLI on your computer: https://devcenter.heroku.com/articles/heroku-cli
-
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
- 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
- Push your app to Heroku (you will see a wall of code)
git push heroku master
- Visit your newly create app by opening it via heroku:
heroku open
- 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"
}
Or look Procfile:
web: npm start
You also have to make changes to the port, you can't hardcode a dev-port. But you can reference heroku port. So the code will have the following:
const port = process.env.PORT || 3000;
Same as Heroku, will sleep after a while.
- Register for Glitch
- Click New Project
- Click Import from GitHub
- Paste the full URL of your repository
https://github.com/lamngockhuong/dummy-api.git
into the URL input and click OK. - Wait for it to setup
- Go to Settings > Edit project details > Change Project Name. Then press Save
- Press Share button to get your URL to live site. It should be something for example like: https://dummy-api-test.glitch.me