npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

biz-mock

v1.0.1

Published

mock center

Downloads

111

Readme

biz-mock

NPM version NPM downloads total NPM license Build Status bitHound Overalll Score Coverage Status Dependency Status devDependency Status

biz-mock支持通过json静态数据,随机模板数据,cookie原酸数据,生成模拟数据

配合独立的server,能够减少前端开发对后端环境的依赖

NPM Stat

功能

  • 拦截ajax请求
  • 三种mock数据源,json,模板和server
  • 支持自定义拦截器

安装

npm install biz-mock --save

使用

var mock = require('biz-mock');

biz-mock首先需要初始化配置文件,以及mock静态文件目录

初始化完成后,开启mock功能

拦截ajax请求

API

initFolder()

initFolder([destPath])

向指定路径复制配置文件以及mock静态文件目录

配置文件mockConfig.json

/mock/config/mockConfig.json

mock静态文件目录:

//mock jsondata folder
/mock/data/

//mock templatedata folder
/mock/template/

start()

start([options])

初始化mock配置,启动mock功能

options

  • as:拦截ajax请求的后缀,默认是'.action'。可以拦截多种后缀的ajax,在as参数中多个后缀用,分隔。如请求不带.*后缀,可以配置成空字符串。

  • mockConfig:配置文件json对象,默认路径是options.root + /mock/config/mockConfig.json

  • silent:是否打印各种log,默认是false

  • methods:拦截ajax请求的method,默认是['post', 'get']

  • root:base路径,默认是process.cwd()

dispatch()

dispatch(req, res)

拦截ajax请求,并返回mock数据

mockConfig

mockConfig.json:

{
"dataSource": ["server", "json", "template"],
"json": {
    "path": "/mock/data/",
    "wrap": false
},
"server": {
    "host": "http://localhost:8080/",
    "serverParams": {
        "index": 1
    },
    "statusCode": [200],
    "rejectUnauthorized": false,
    "secureProtocol": "SSLv3_method",
    "cookie": "",
    "proxy": ""
},
"template": {
    "path": "/mock/template/"
}

}

mock数据源

dataSource是mock数据源的集合,目前有原生提供四种数据源cookie,template和json以及mockserver

数据源在集合中的排序规定了数据源的优先级,索引越小,优先级越高。如上面例子中,会首先查找cookie数据源,如果cookie数据源没有数据,接着是mockserver数据源,如果还没有,那么会查找template数据源,仍然没有,继续查找json数据源。四种数据源都没有的话,会返回404

1.json

json是静态数据源

"json": {
	"path": "/mock/data/",
	"wrap": false
}

path:静态数据源文件目录

wrap,数据外层是否被包裹(兼容处理)

包裹格式:

{
    "enabled": true,
    "value": "success",
    "success": {}
}

2.template

template是通过数据模板生成模拟数据

生成器选用http://mockjs.com/

3.server

server是通过在配置文件中配置host,cookie,额外参数serverParams等,实现从其他服务中获取请求数据的功能

配置文件如下:

"server": {
    "host": "http://localhost:8080/",
    "serverParams": {
        "index": 1
    },
    "statusCode": [200],
    "rejectUnauthorized": false,
    "secureProtocol": "SSLv3_method",
    "cookie": "",
    "proxy": ""
}
  • host:访问域名,支持http和https
  • serverParams: 可能额外需要的参数
  • statusCode: 配置需要返回结果的响应码,默认是200,如果服务的响应码不在此列表中,则从其他mock源获取数据
  • secureProtocol:SSL协议,根据安装的OpenSSL设置。比如SSLv3_method,即设置为SSL第三版。具体可参考SSL_METHODS
  • cookie: cookie
  • proxy:代理

json和template路径与请求路径一致,例:

请求:/query/table.action

json路径/mock/data/query/table.json

template路径/mock/data/query/table.template

如何自定义mock数据源

mock数据源实现getData方法

exports.getData = function(url, req, res, cb){
	return data
}

方法参数:

  • url:http请求,注意,不带后缀
  • req: request
  • res: response
  • cb: biz-server采用co控制异步操作的流程,自定义数据源会被thunkify,cb是co的回调函数,cb(error, data)

方法返回:

  • json数据

另外需要在/mock/config/mockConfig.json的dataSource注册mock数据源

更新日志

0.0.5

  • 修复suffix设置为''时存在的问题
  • 修复suffix逻辑中正则表达式匹配不准确的问题
  • 修复cookie模式下当request header中content-type为application/json时出错的情况
  • 优化/config/mockConfig.json文件的目录结构,将其移入/mock/config/mockConfig.json
  • 修改initFolder逻辑,不再生成/config目录
  • 初始化时,当未设置config文件路径时,优先使用/mock/config/mockConfig.json, 同时如果新的不存在则兼容老的路径,并给出dperaciate提示。建议升级后进行迁移,简化工程目录
  • 新增配置项的热更新,避免切换数据源时需要重新启动应用
  • 代码格式化

0.1.1

  • 把cookie mock源升级为server源,通过在配置文件中配置host,cookie,额外参数serverParams等,实现从其他服务中获取请求数据的功能
  • server源可以用列表方式配置多种配置,会依次尝试直至获取结果
  • 更改package.json的test脚本,使windows环境也能启动