GithubHelp home page GithubHelp logo

ufologist / puer-mock Goto Github PK

View Code? Open in Web Editor NEW
500.0 27.0 64.0 520 KB

Puer + Mock.js = A configurable mock server with configurable mock(random) data.

Home Page: http://ufologist.github.io/puer-mock/

License: MIT License

JavaScript 49.85% HTML 50.15%
mock-server

puer-mock's Introduction

puer-mock

NPM version changelog license

Puer + Mock.js = A configurable mock server with configurable mock(random) data.

Start a mock server never ever so easy and configurable, no code needed, you only need config some mock API route and some mock data template, it all done!

features

  • mock server
  • configurable RESTful API route
    • API request
    • API response mock data
    • validate API request params
  • effective immediately when config changed
  • JSONP support
  • CORS support
  • built-in API doc

install

Please install puer first.

Puer - more than a live-reload server , built for efficient frontend development

npm install [email protected] -g

And then install puer-mock package in your project root directory.

cd yourprojectdir
npm install puer-mock

If your project has a package.json file, suggest save puer-mock to devDependencies.

cd yourprojectrootdir
npm install puer-mock --save-dev

usage

  1. copy node_modules/puer-mock/example files to your project root directory

  2. cd yourprojectrootdir

  3. puer -a _mockserver.js

    You may define a npm script in package.json, then use npm run dev next time.

    "dev": "puer -a _mockserver.js"
  4. view mock API

  • open http://localhost:8000/api/configdemo-response/mock view mock API return mock data
  • open http://localhost:8000/api/configdemo-response/mock?callback=test view mock API return mock data by JSONP
  • open http://localhost:8000/_apidoc.html view built-in all mock API doc
  1. define your mock API in _mockserver.json
  2. view your mock API(Yes! it with immediate effect)

config

Config mock API in _mockserver.json.

The minimal _mockserver.json

{
    "api": {
        "GET /api/users": {
            "response": {}
        }
    }
}

Please view mockserver config doc for more detail info.

You can see more settings to config mock server in _mockserver.json.

  • comment is nice
  • config route and mock response data template is so easy
  • more data template please see Mock.js examples
  • disable a route is convenient
  • mock server will effective immediately when you changed _mockserver.json

customize

Customize launch mock server in _mockserver.js.

  • You may customize mock API doc

    • You may customize /_apidoc route direct service a more graceful doc

      // _mockserver.js
      module.exports = require('puer-mock')(null, null, function(mockConfig) {
          // 任你自由发挥
          return '<pre>' + JSON.stringify(mockConfig, null, 4) + '</pre>';
      });
    • You may customize /_apidoc.html which get mock API JSON from /_apidoc, you may totally implement it by yourself

  • You may customize config file and launch file

    If you do not use default _mockserver.json or _mockserver.js

    // use _mockserver2.json for config file
    //
    // _mockserver.js
    // puer -a _mockserver.js
    module.exports = require('puer-mock')(__filename, './_mockserver2.json');
    // use _mockserver2.js and _mockserver2.json
    //
    // _mockserver2.js
    // puer -a _mockserver2.js
    module.exports = require('puer-mock')(__filename, './_mockserver2.json');
  • Play it fun with your imagination.

    // _mockserver.js
    var puerMock = require('puer-mock');
    var routeConfig = puerMock();
    // 其实你完全可以这样来玩, 在这里接着定义 route 即可, 发挥你的想象力
    // 例如定义一个常用的 500 接口, 来测试服务器报错的情况
    routeConfig['GET /500'] = function(request, response, next) {
        response.status(500).end();
    };
    module.exports = routeConfig;

example

眼见为实耳听为虚, puer-mock 让你一秒钟就能拥有一个强大的 mock server, 所以请不要再自己手工做假数据了, 赶快尝试一下让你的工作效率翻番吧!

配置 API 并生成随机数据

puer-mock-example

查看所有的 API 接口文档

通过 /_apidoc.html 查看接口文档

puer-mock-api-doc-html

通过 /_apidoc 直接查看 JSON 数据, 即查看 mock server 配置了哪些 API(使用 FeHelper 插件 格式化了 JSON 数据)

puer-mock-api-doc

one more thing

谁需要这个工具

感谢 puer 提供了如此强大的 mock 机制, 感谢 Mock.js 带来的假数据, 我才有幸做了这么一个扩展工具.

puer-mock's People

Contributors

ufologist avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

puer-mock's Issues

TypeError: Cannot read property 'displayName' of undefined

启动服务器后,每次保存 _mockserver.json 文件都会报这个错误,

{17-08-28 15:53}Leung:~/work/api lynnleung% puer -a _mockserver.js
{ addon: '/Users/lynnleung/work/api/_mockserver.js' }
log:	 watcher on!! 	 2017-08-28  15:53:30
log:	 addon update !!!/Users/lynnleung/work/api/_mockserver.js 	 2017-08-28  15:53:30
log:	 server start at localhost:8000 	 2017-08-28  15:53:30
log:	 puer will launch your default browser 	 2017-08-28  15:53:31
/usr/local/lib/node_modules/puer/node_modules/weinre/lib/utils.js:183
    funcName = func.displayName || func.name || callSite.getFunctionName();
                   ^

TypeError: Cannot read property 'displayName' of undefined

启动puer报错,但可正常使用

环境:Centos7 安装完puer 后,启动 出现以下错误,但是可以正常使用。何解?
**(process:24308): GLib-CRITICAL : g_slice_set_config: assertion 'sys_page_size == 0' failed
Error: no display specified

Node v10.15.1环境下修改配置文件报错

fs.js:137
throw new ERR_INVALID_CALLBACK();
^

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
at makeCallback (fs.js:137:11)
at Object.utimes (fs.js:1090:14)
at /mnt/h/Projects/waper/subject.waper.com/node_modules/puer-mock/src/puer-mock.js:34:12
at StatWatcher. (/mnt/h/Projects/waper/subject.waper.com/node_modules/puer-mock/src/util.js:25:24)
at StatWatcher.emit (events.js:189:13)
at StatWatcher.onchange (internal/fs/watchers.js:50:8)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] mockServer: npx puer -p 9090 -r puer/puerfile.js "puer/config.js"
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] mockServer script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/richard/.npm/_logs/2019-02-20T09_08_28_867Z-debug.log

run puer script error

when I run script, I get Store.prototype.__proto__ = EventEmitter.prototype; Cannot read property 'prototype' of undefined. my node.js version is v7.5.

能否用 js 写 mockserve.json?

主要想法是 想 用多个 js 文件,来作为 mock 的 template 模板。
之后,再统一引用到 mockserver.json 文件中,
把 mockserver.json 的文件格式变成 js 的话,就可以对象的形式引用 mock template 的 js 模板文件了。

表达的可能不太清楚,盼回复。谢谢。

pure -a throw error

package.json
puer -a _mockserver-config.js

当我执行这段命令,会报 “unknow option -a“ ,puer -h 里查看,确实也查不到-a的后缀命令。
但看puer的官网example上也是有-a的例子,求帮助

一些疑问?

就是我司后台用的是swagger。

然后怎么结合这个mock server?

文档里面提及的sosoapi,具体是做什么的?因为对于后台来说,文档已经生成了,不需要手动再编写一份了吧?

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.