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 🙏

© 2025 – Pkg Stats / Ryan Hefner

zen-mockserver

v0.0.3

Published

zen mockserver

Downloads

5

Readme

Zen Logo

zen-mockserver

一个实用的mock工具

运行环境

由于我们的开发使用的都是MacOS/Linux系统,Windows下没有严格测试,稍后会支持。

安装

$ npm install -g zen-mockserver

也可以仅在项目中使用:

$ npm install zen-mockserver --save-dev

快速使用

  1. 创建一个空文件夹,然后在该文件夹下初始化mockserver所需的配置文件
$ mkdir demo-project && cd demo-project
$ mock init 

会在当前文件夹下初始化配置文件,你将看到生成了如下的目录结构:

.
├── config
│   ├── index.js
│   └── local.js
├── data
│   └── demo.json
├── middleware
│   └── monitor.js
├── swagger-template
│   └── index.json
└── utils
    └── index.js 
  1. 编写Mock Data 在data文件夹新建文件demo-students.json,输入如下内容:
{
  "method": "get",
  "url": "/api/students",
  "response": {
    "status": 0,
    "msg": "success",
    "data|4-10": [
      {
        "name": "zhangsan",
        "age|+1": 13
      }
    ]
  }
}

并保存。

上述我们使用了data|4-10age|+1mockjs语法,以便生成随机数据,

  1. 启动服务
$ mock start

浏览器访问 http://localhost:9000/api/students 查看API

注意: 上面不一定是9000端口,如果9000端口号被占用,mock工具则会使用9001端口,以此类推

  1. 生成swagger
$ mock swagger

会在当前目录下生成swagger-data.json文件,将文件内容粘贴至swagger编辑器中即可查看结果。

配置说明

zen-mockserver(以下简称mock)配置文件分为三部分,可以参考mock init生成的目录:

  • config:用于端口、代理等全局配置
  • middleware:可根据需求自定义中间件
  • data:自定义mock数据配置, 如demo.json:
{
  "method": "get",
  "url": "/person",
  "response": {
    "status": 0,
    "msg": "success",
    "data|4-10": [
      {
        "name": "zhangsan",
        "age|+1": 13
      }
    ]
  }
}

上述data|4-10使用的是mockjs语法。

如何配置代理

配置文件默认在config/local.js 下面,你可以设置环境变量MOCK_CONFIG_FILE_NAME,eg:MOCK_CONFIG_FILE_NAME=my,则mockserver会加载config/my.js下的配置文件。

  • port [number] 默认启动mockserver的端口
  • proxy [array] 代理配置

示例配置

module.exports = {
  port: 9000,
  proxy: [
    // 接口转发
    {
      path: '/posts',
      target: 'https://jsonplaceholder.typicode.com',
      pathRewrite: {
        '^/posts' : '/photos',     // rewrite path
      },
      headers: {
        cookie: 'sessionid=1001',
      }
    },
    // 不以/api 开头的请求转发到 http://localhost:3000/
    // 用以转发静态资源的请求
    {
      path: '^(?!/api)',
      target: 'http://localhost:3000/',
    },
  ]
};

贡献代码

欢迎大家贡献代码 : )