Giter Site home page Giter Site logo

ajoelp / mock-json-server Goto Github PK

View Code? Open in Web Editor NEW
31.0 3.0 8.0 869 KB

A mock web server using a JSON file with live-reload support.

License: MIT License

JavaScript 72.17% HTML 11.51% Dockerfile 1.71% Shell 8.77% HCL 5.85%
mock-server json node javascript http http-server live-reload

mock-json-server's Introduction

Mock Json Server

code style: prettier Build Status

Create a mock server using a json file.

Installation

To install, you need to have NodeJS and NPM installed on your system. Go to https://nodejs.org/en/, download and install one of the provided versions. Both NodeJS and NPM are included.

Install the mock-json-server package by running npm install -g mock-json-server in your terminal.

Using npm v5.6.0 or later? You can skip global installation and directly run npx mock-json-server data.json in a folder containing the below explained data.json.

Thats it!

Example

To run the server run: mock-json-server data.json This starts a server on http://localhost:8000/ you can change the port by running mock-json-server data.json --port=3000

data.json contains:

{
    "/home": {
        "get": {
            "data": [
                {"id":1,"name": "Steve"}
            ]
        },
        "post": {
            "data": [
                {"id":1,"name": "Steve French"}
            ]
        }
    }
}

A Get Request to http://localhost:8000/home will return.

    {
        "data": [
            {"id":1,"name": "Steve"}
        ]
    }

A Post Request to http://localhost:8000/home will return.

    {
        "data": [
            {"id":1,"name": "Steve French"}
        ]
    }

Docker

Mock json server is also a docker image. Run it with the command

docker run --name mock-json-server -v $(pwd)/test/data.json:/usr/src/app/data.json -p 8000:8000 ajoelpod/mock-json-server

Changing the port

Run the same command as above but with the PORT environment variable. Also change the -p to be equal to your new port.

docker run --name mock-json-server --env PORT=3000 -v $(pwd)/test/data.json:/usr/src/app/data.json -p 3000:3000 ajoelpod/mock-json-server

or

Just change the forwared ports

docker run --name mock-json-server -v $(pwd)/test/data.json:/usr/src/app/data.json -p 3000:8000 ajoelpod/mock-json-server

Docker Compose

Example.

version: '3'
services:
  mock-json:
    image: 'ajoelpod/mock-json-server'
    volumes:
      - ./test/data.json:/usr/src/app/data.json
    ports:
      - 8000:8000

Running Programmatically

const server = require('mock-json-server');
const app = server({
  "/home": {
    "get": {
      "data": [{ "id": 1, "name": "Steve" }]
    },
    "post": {
      "data": [{ "id": 1, "name": "Steve French" }]
    }
  },
  "/test/:id": {
    "get": {
      "data": [{ "id": 1, "name": "Steve" }]
    },
    "post": {
      "data": [{ "id": 1, "name": "Steve French" }]
    }
  }
}, 8000); // Start the server with a JSON object;


// Start the server;
app.start();

// Reload the server with new data;
app.reload({ test : true });

// Stop the server
app.stop();

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.