Coder Social home page Coder Social logo

mock-server's Introduction

Mock Server

what is it ?

a simple mock server built on express, totally based on file system and dynamic reload

how to use

git clone https://github.com/skidxjq/mock-server
cd mock-server && npm install
npm run mock

and then open Mock Manager Manager

examples

demo1

if you want to create a API named '/m/demo/one', you should create a folder totally the same with API except for replacing '/' with '-', then you could create an index.js file in this folder, and make your mock data as default export.

cd data && mkdir m-demo-one
touch index.js
echo 'module.exports = ${your_data}' > index.js

file conent in data/m-demo-one/index.js

module.exports = {
  "code": 0,
  "msg": "success",
  "data": {
    "name": "skidxjq",
    "age": 18
  }
}

demo2

if you want to create a API named '/m/demo/two', serveral alternatives of mock data are prepared, you may switch one to another, just shown as image below.

if you want to mock your data in three ways how to do it

cd data && mkdir m-demo-two
touch A.js B.js C.js
echo 'module.exports = ${data_A}' > A.js
echo 'module.exports = ${data_B}' > B.js
echo 'module.exports = ${data_C}' > C.js

how to configure

cd data && mkdir m-demo-two
touch map.json
echo ${config_data} > map.json

the content of map.json should be like as below, keys should be in line with names of data files in folder interface delay describe the delay ms time (default 0)

{
  "desc": "this is just a description",
  "keys": {
    "A": "data A",
    "B": "data B",
    "C": "data C"
  },
  "delay": 0
}

change current mock data via http://localhost:8080/interface-admin/public/index.html

demo3

make a response based on the request parameters, such as check req.body or validate your parameters submitted from a html form how to do it . see examples in data/m-demo-three.

Advantages

  • Do not need to restart your server, our core principle is url pattern matching, and make it like a express router
  • custom your own response based on request parameters
  • dynamicly switch one data to another on dashboard

Cautions

  • There are no ways to nest your APIs, you should relate one api folder to the specified api name .
  • In this example, we defined the specified started with 'm-' matches those folders and apis, in your application, you could change or remove it on your own.

mock-server's People

Contributors

skidxjq avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.