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

express-mock-proxy

v1.2.5

Published

A mock proxy with express

Readme

express-mock-proxy

利用express、mockjs和express-http-proxy开发的接口mock服务端,几行代码就可以搭建起一个接口mock服务,有如下特点:

  • 通过js文件来配置接口mock数据,包括url、method、延时、http响应状态、响应内容都可以配置
  • 内部集成mockjs,可以方便的生成模拟数据
  • 复杂的业务逻辑可以通过返回函数,编码实现
  • 在开发时,可能只有部分接口需要mock,其他请求都正常访问后端服务器,可以在启动mock服务时, 指定一个后端的接口地址,所有未mock的接口请求,都会被转发到后端服务器

使用前最好先熟悉mockjs相关的使用。

快速开始

启动

新建server.js。

// server.js
const mockProxy = require('express-mock-proxy');
const Mock = require('mockjs');

const app = mockProxy(3001, null, 'http://api.test.com', Mock);

运行该代码,将在3001端口启动mock服务,并将所有请求转发到http://api.test.com

node server.js

添加一个mock接口

新建routes目录,我们将所有的mock数据都放在该目录下,在routes目录下新建books.js

// routes/books.js
const books = [
  {
    url: '/api/v1/books',
    disabled: false,
    method: 'get',
    delay: 20,
    status: 200,
    data: {
      name: '@ctitle',
      author: '@ctitle',
      'pageNum|100-1000': 0,
      'wordNum|10000-100000': 0,
      publishAt: '@data',
    },
  },
];

module.exports = books;

然后在server.js中添加routes目录,系统会自动扫描routes下(包括子目录)的所有js文件, 将符合格式的js文件中的mock数据加载到express路由中。

指定扫描目录:

const path = require('path');
const dir = path.join(__dirname, 'routes');
const app = mockProxy(3001, dir, 'http://api.test.com', Mock);

启动服务后,访问http://localhost:3001/api/v1/books 就可以看到mock数据了。

接口文件定义

接口文件返回规则数组:

const sample = [
  {rule1  },
  {rule2  },
  {rule3  },
];

module.exports = sample;

规则字段定义:

  • url,接口url地址,必填
  • disabled,是否禁用,默认为false
  • method,http方法,默认为get
  • delay,模拟响应延时,默认为300ms
  • status,响应http状态码,默认为200
  • data,响应数据,可以是对象(mockjs)或函数

data函数

如果需要mock的内容过于复杂,或者与请求中的参数相关,可以通过定义函数来实现。

下面我们定义添加

const books = [
  {
    url: '/api/v1/books',
    method: 'post',
    status: 200,
    data: function ({req, res, Mock, Random}) {
      // 从express的req中获取请求参数
      const { page } = req.query || {};

      // 返回{status, delay, content}
      return {
        status: 400,
        delay: 1500,
        content: {
          code: 99,
          message: '@title',
        },
      };
    },
  },
];

module.exports = books;

data函数的3个参数分别是:

  • req,express的req对象
  • Mock,mockjs对象
  • Random,Mock.Random

上面的例子中的,除了响应内容content外,我们重新设置了接口的statusdelay参数, data函数返回的参数会覆盖掉原有的设置。

自定义mockjs占位符

在启动服务之前,可以先设置自定义的占位符,然后就可以在接口文件中使用了。

const Mock = require('mockjs');
const Random = Mock.Random;
Random.extend({
  myname: function () {
    return Random.pick(["jim", "tom"]);
  }
});

const app = mockProxy(3001, 'routes', 'http://api.test.com', Mock);