Giter Club home page Giter Club logo

mocknobody's Introduction

mocknobody

npm version Dependencies Known Vulnerabilities

Creates api server in your local machine with 1 file route configuration. Live-reloads the mock server if API config changes. Does not do any process on request body (hence mocknobody).

Installation

npm i -g mocknobody

Creating API file

mocknobody -c

This will create api.js file, located by default in $HOME/.mocknobody/api.js. If you prefer to use your own api.js file, you can override this location by adding API_JS env variable e.g. API_JS=/var/mock/api.js mocknobody. api.js sample can be seen on api_sample.js.

Basic Usage

To start your mock server:

mocknobody

In separate terminal, you can try this example request

curl localhost:3000/ping

(if the command mocknobody is too long, you can add alias mock=mocknobody on your .bashrc or .zshrc file and use mock instead.)

api.js structure

api.js has this structure:

module.exports = {
  "routes": [
    {
      "method": "GET",
      "url": "/ping",
      "status": 200,
      "response": "pong",
      "headers": { 
        "content-type": "application/json",
        "x-test": "123"
      },
    },
  ],
  "not_found": {
    "response": "not found",
  },
}
  • routes: array of route on your API, defined by these values:
    • method: case-insensitive http methods. Currently supports get, post, put, patch and delete.
    • url: relative url of your API. You can use same URL with different param queries in different routes.
    • status: integer of HTTP Response status code
    • response : can be string or a javascript object (will be returned JSON as default)
    • headers : object of additional response headers you want to send. you can override existing response header or create new one.
  • not_found: default response if the route is not found

You can check out tests/api_test.js for more examples.

Since api.js is imported just like normal javascript file by the server, you can add any logic on the file as you want. As long as the format exported is still the same as defined above, mocknobody will be able to parse them.

Configurations

All these options can be configured through environment variables

Property Description Default value
PORT Port to serve the mock server 3000
API_JS Overriding API_JS file with your own file $HOME/.mocknobody/api.js

Example usage with config:

PORT=3001 API_JS=/var/mock/api.js mocknobody

Running on Development mode

DEV=true ./src/wrapper.js

this will run mocknobody.js in src, as well as adding it and wrapper.js to file watch list.

Design decisions

  • why use api.js and not api.json?
    • In addition to enable you to be flexible with your logic by having javascript file instead of just json file, this will also enable you to comment out any routes/responses/anything while you develop your frontend. This is a tool for mocking APIs after all.

License

MIT

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.