Giter Site home page Giter Site logo

lamngockhuong / dummy-api Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 67 KB

Create Dummy API Server by json-server

Home Page: https://dummy-api-test.herokuapp.com

License: MIT License

Procfile 0.27% HTML 37.34% JavaScript 34.94% CSS 25.50% Dockerfile 1.95%
json-server dummy-api json-server-heroku json-server-example json-server-glitch

dummy-api's Introduction

dummy-api

Dummy API

Create your database

  1. Press Use this template button in the right corner
  2. Grive your new repository a name and press Create repository from template button
  3. Clone your newly created repository to your computer
  4. Change the contents of db.json and routes.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 have id, name and email
// db.json
{
  "users": [
    {
      "id": 1,
      "name": "Ngoc Khuong",
      "email": "[email protected]"
    }
  ]
}
  • This example will define a custom route, search user by email
// routes.json
{
  "/users/email/:email": "/users?email=:email"
}
  1. If you want to change home page template, please update the contents at public/

Run on Docker

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 container
  • lamngockhuong/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

Deploy

Deploy to Heroku

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

Install Heroku and deploy

From Dashboard
  1. Create your database
  2. Create an account on https://heroku.com
  3. Login and Create new app
  4. Go to Deploy tab > Deployment method > Choose GitHub
  5. Go to Connect to GitHub, connect to your github account and search your repository > Click Connect
  6. Go to Manual deploy > Choose a branch to deploy > Press Deploy Branch
  7. Go to Overview tab to view build log and more.
From CLI
  1. Install the Heroku CLI on your computer: https://devcenter.heroku.com/articles/heroku-cli

  2. 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
  1. 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
  1. Push your app to Heroku (you will see a wall of code)
git push heroku master
  1. Visit your newly create app by opening it via heroku:
heroku open
  1. For debugging if something went wrong:
heroku logs --tail
How it works

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;

Deploy to Glitch

Same as Heroku, will sleep after a while.

  1. Register for Glitch
  2. Click New Project
  3. Click Import from GitHub
  4. Paste the full URL of your repository https://github.com/lamngockhuong/dummy-api.git into the URL input and click OK.
  5. Wait for it to setup
  6. Go to Settings > Edit project details > Change Project Name. Then press Save
  7. Press Share button to get your URL to live site. It should be something for example like: https://dummy-api-test.glitch.me

Refer

dummy-api's People

Contributors

khuongln-1346 avatar lamngockhuong avatar renovate[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

dummy-api's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
Dockerfile
  • node 18-alpine3.14
github-actions
.github/workflows/main.yml
  • actions/checkout v3
  • actions/setup-node v3
.github/workflows/push.yml
  • actions/checkout v3
  • actions/setup-node v3
  • docker/setup-buildx-action v2
  • actions/cache v3
  • docker/login-action v2
  • docker/build-push-action v3
npm
package.json
  • json-server 0.17.0
  • node 16.x
  • npm 8.x

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.