JSON Server Sample

by VicAv99

GitHub Readme.md

Deploy json-server to a free hosting website

Instructions how to deploy the full fake REST API json-server to various free hosting sites. Should only be used in development purpose but can act as a simpler database for smaller applications.

Create your database

1 . Clone or fork this repo to anywhere on your computer. (Ideally, you would have a dedicated "projects" directory)

  git clone https://github.com/VicAv99/hosted-json-server.git

2 . Update db.json to your own content according to the json-server example and then commit your changes to git.

this example will create /posts route , each resource will have id, title and content. The id will auto increment! hint: start from 1

  {
    "posts":[
      {
        "id"  :  0,
        "title":  "First post!",
        "content"  :  "My first content!"
      }
    ]
  }

Deploy to Heroku

Heroku

Heroku is a free hosting service for hosting small projects. Easy setup and deploy from the command line via git. You can also skip the steps below and just click this button -> Deploy

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, but will still need to "sleep" for six hours on free apps

Install Heroku

1 . Create your database 2 . Create an account on https://heroku.com 3 . Install the Heroku CLI on your computer:

If you are using brew,

  brew install heroku/brew/heroku

or you can see the heroku documentation here at 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 . Push your app to Heroku (you will see a wall of code)

  git push heroku master

7 . Visit your newly create app by opening it via heroku:

heroku open

8 . 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"
  }

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;

Deploy to now

1 . Create your database 2 . Install now cli-tool globally

  npm install -g now

3 . Run the now command in this folder/repo where your project is. If you run it for the first time, you will be prompted to login, after login, run the command again:

now --public

--public is used to skip the prompt telling you that you will open source your project if you deploy it to now

4 . The URL will be copied automatically and you can just paste it into your browser. 5 . Optional: Rename the deployment:

  now alias https://your-deployed-name.now.sh new-name

first argument is the deployed site, second argument is the new name to give it


Deploy to UP

You can also use Apex UP to deploy a smaller app for free.

The pros are that on UP the app will be deployed as a Lambda function. That way if you need a secure api for a private project you can do so.

Installation

2 . Install the up-cli:

https://up.docs.apex.sh/

  curl -sf https://up.apex.sh/install | sh

3 . Verify the installation:

  up version

? Readme and app added from json-server and jesperorb.