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

mock123

v0.1.1

Published

使用最简单、快捷的方式开启mock api之旅

Readme

mock数据服务

1、概述

一个基于node.js的轻量级API mock服务。

  • 支持GET和POST请求、支持跨域
  • mock文件支持 .js 文件和 .json 文件
  • json文件 用来存放固定的模拟数据,而 js文件 可更加自由的处理并返回模拟数据
  • 修改mock文件后,无需重启node服务,重新调用接口即返回新的数据

 

2、安装

npm install mock123 -g

3、启动服务

在存放mock数据的文件夹下执行命令启动服务。

如果mock数据文件放在名为 mock-data 的文件夹下,则在mock-data文件夹下执行以下命令。

以下命令默认端口为 7777

mock123 start

或者使用自定义端口

mock123 start 6666

服务启动后,访问地址为

http://localhost:7777

4、创建mock数据文件

在存放mock数据的目录下新建 .json 或者 .js 为后辍的文件。

mock规则示例如下:

如果对同一接口分别创建了jsjson两个mock文件,会优先取js文件的数据。

1) 命名规则

mock文件名必须和API接口的文件名一致,才能将API和mock文件匹配。

例1:

如果api接口为 /service/user/getUserInfo

则mock数据文件名为 getUserInfo.js 或者 getUserInfo.json

例2:

如果api接口为 /service/user/getUserInfo.do

则mock数据文件名为 getUserInfo.do.js 或者 getUserInfo.do.json

2)文件内容

  • API接口名.json 文件只支持json格式的数据

示例:

{
  "code": 1,
  "msg": "message...",
  "data":{
      "age": 520,
      "card": 10099
  },
  "servertime": 1534835882204
}
  • API接口名.js 文件必须导出一个函数,并在导出的函数中返回mock数据。函数接收两个参数

示例:

/*
    返回mock数据

    @param {object} getData  api接口接收到的GET数据
    @param {object} postData api接口接收到的POST数据
 */
module.exports = (getData, postData) => {
    let data = {}
    let name = getData.type === 1 ? 'GG' : 'MM'

    if(postData.userId === '123'){
        data = {
            code:1000,
            msg: 'message...',
            data:{
                name:"A",
                age: 50
            }
        }
    } else {
        data = {
            code:2000,
            msg: 'message...',
            data:{
                name:"B",
                age: 80
            }
        }
    }

    data.__data__ = {
        get: getData,
        post: postData
    }

    return data
}

5、完整示例

1) 先安装包

npm install mock123 -g

2) 在任意位置新建一个文件夹 mock-test

3)mock-test 目录下新建mock数据文件 test.json,并添加内容如下:

{
  "code": 1,
  "msg": "message...",
  "data":{
      "age": 520,
      "card": 10099
  },
  "servertime": 1534835882204
}

4)mock-test 目录下面执行命令启动mock服务 (默认端口7777)

//如果启动服务出错,有可能是端口冲突,需更换端口
mock123 start

5) 打开浏览器,输入以下地址,访问mock文件

http://localhost:7777/api/test
http://localhost:7777/api/aa/bb/test
http://localhost:7777/service/aa/test
http://localhost:7777/abc/test

请求地址只要以test结尾的,都可以访问到上面创建的 test.json 文件

6) 项目中使用

以webpack4为例:

//接口host地址,区分生产环境和测试环境
apiBaseURL = process.env.NODE_ENV === 'production'
    ? 'https://api.github.com'
    : 'http://localhost:7777'

api = apiBaseURL + '/api/user/test'