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

awesome-mock

v2.0.6

Published

mock server

Readme

awesome-mock项目说明

目录结构

src                             # 项目源码目录
  |--index.js                   # 入口文件 
  |--core.js                    # 主逻辑文件
  |--tool.js                    # 工具文件

关于awesome-mock

  • 提供Listen和Mock两种使用模式,通过配置文件切换。
  • 接入Mock.js,支持动态生成Mock数据
  • Mock返回值除基本格式外,还支持自定义函数。可用于设置响应头数据
  • 提供工具方法,封装了一些模拟请求时可能要用到的场景

配置文件说明

  • 服务会自动读取项目根目录下的.mock-server.js配置文件,也可通过cli参数configPath指定目录
  • 参数说明
export default {
  port: 3000,   // mock服务端口,默认:3000
  mockPath: '/mock', // mock目录,服务会自动遍历一级目录下所有的非'_'开头的js文件内的数据。默认:'/mock'
  useTempMock: false, // MockMode下,是否使用_temp内数据作为fallback引用,默认:false
  listen: '', // ListenMode下需要转发的服务器地址,当有值时会自动切换到ListenMode,默认:null
  headers: {   // ListenMode下自定义请求头,如cookie,不支持cli命令参数,默认:{}
    'cookie': 'SESSION=OGNmMDVjMjUtNDIwZC00MzIwLWFjOTMtYzkyOWIyYzc2MjU5'
  } 
}

ListenMode与MockMode

  • 当配置文件配有listen字段时,服务会自动切换到ListenMode, 此模式主要作用是用于快速收集接口真实数据,浏览器在发起接口请求时会自动转发请求到真实服务器, 并将返回的数据保存到mockPath目录下的_temp目录,此数据可供MockMode使用。
  • MockMode下,服务会自动遍历收集一级目录下所有js文件内的接口数据,当浏览器发起请求时, 匹配对应数据进行返回。返回值支持通过Mockjs动态生成,支持自定义函数。
  • MockMode下,useTempMock启用后,当一级目录下的接口数据无匹配结果时,会尝试使用 _temp目录下的接口数据作为fallback。

如何使用

  • yarn add awesome-mock
  • 项目内创建.mock-server.js配置文件
  • 创建mock目录与mock文件
const { Mock, Tool } = require('awesome-mock');

module.exports = {
  'GET /timestamp1': {
    success: true,
    code: '200',
    msg: 'success',
    retry: false,
    data: 1579413764667,
  },
  'GET /timestamp2': Mock.mock({
    success: true,
    code: '200',
    msg: 'success',
    retry: false,
    data: '@datetime',
  }),  
  '/kudev-core/project/page-list': async (req, res) => {
    await Tool.sleep(1500);
    Tool.loss(30);
    res.send(
      Mock.mock({
        success: true,
        totalPage: 3,
        [`items|${req.query.pageSize}`]: [
          {
            id: '@id',
            name: '@ctitle',
            owner: '@cname',
            'projectType|1': req.query.projectType
              ? [+req.query.projectType]
              : [0, 1, 2, 3, 4, 5, 6, 7],
            'publishType|1': ['ORDERED', 'INDEPENDENT'],
            'status|1': req.query.status ? [+req.query.status] : [1, 2, 9],
            updatedAt: 1584687712479,
            createdAt: 1584686712479,
            detail: '@sentence',
          },
        ],
      }),
    );
  },
};
  • .mock-server.js配置mockPath目录位置
  • 执行命令mock-server,项目内baseUrl指向mock服务器