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

cf-mock-server

v1.0.20

Published

A flexible and fast mock server

Downloads

8

Readme

Mock Server

travis-ci Coverage Status npm npm node version license

一个轻量化的本地mock服务器

Features

  • 支持中间件方式挂载服务,可用于Express和Koa搭建的Node服务
  • 支持命令行方式启动服务
  • 支持Node启动Express服务作为Mock Server
  • 支持监听配置文件和数据文件,修改热更新
  • 支持请求参数配置使用正则表达式
  • 支持RESTful风格的API, 路径可通过通配符匹配
  • 支持自定义函数处理返回结果, 可自定义Response Header, 模拟延迟等

Install

CLI

// Install
npm install -g cf-mock-server

// CLI
Usage: mock [options] <file ...>


Options:

  -V, --version        output the version number
  -p, --port <port>    Define the mock server started port, default is 8008
  -c, --config <file>  Custom profiles, default is mock/config.js
  -h, --help           output usage information

Module

// Install
npm install cf-mock-server --save-dev

Usage

搭配Vue-cli项目

Vue CLI 3

Vue Cli 3 的项目可通过配置vue.config.js将mock服务以中间件的形式挂载到开发服务器上

// mockserver是单独的node服务采用proxy方式
module.exports = {
  devServer: {
    proxy: 'http://localhost:8008', // will proxy all request
  }
}

// 也可以以中间件形式挂载到webpack-dev-server上
const mock = require('cf-mock-server/express-mw')
const path = require('path')

const options = {
  config: path.join(__dirname, './mock'),
  watchs: [path.join(__dirname, './mock')] // 监听mock目录下的所有js文件,热更新
}

module.exports = {
  devServer: {
    clientLogLevel: 'info',
    after: (app) => {
      app.use(mock(options))
    }
  }
}

Vue CLI 2.x

之前版本的Vue Cli只能单独启动一个mock服务器, 通过配置proxyTable将开发服务器的请求代理到mock服务器

// mock.js
const path = require('path')
let Mock = require('cf-mock-server')

let app = new Mock({
  config: path.join(__dirname, './config'), // 配置文件
  watch: true, // 观察模式,监听配置文件改动自动应用
})

app.run()

// config/index.js
dev: {
  proxyTable: {
    '/api': 'http://127.0.0.1:8008', // will proxy request with '/api' prefix
  },
}

API配置文件

支持JSON和JS格式

// config.js
// 数据文件根目录,如果API数据是文件路径则需要配置此项
exports.dataFile = '../{YOUDATAFOLDER}'
/**
 * API配置, 路径支持通配符,参数支持通配符,正则(右斜杠需要两个)和不完全匹配
 * KEY: '{METHOD} {router}'
 * VALUE: JSON字符串或数据文件路径,相对于dataFile, 可省略扩展名
 */
exports.api = {
  'GET /api/users/all': 'all_users',  // only match /api/users/all
  'GET /api/users/all?name=sam': 'users/example', // => /api/users/all?name=sam&age=18
  'GET /api/users/?name=*': 'users/tom.json', // => /api/users/?name={anyone}
  'GET /api/v1/users/tom': 'users/tom.json',// only match /api/v1/users/tom
  'GET /api/v1/users/*': 'users/example', // => /api/v1/users/{anyone}
  'GET /api/v1/*/tom': 'users/tom.json', // => /api/v1/{anyone}/tom
  'GET /api/**/tom': 'users/tom.json', // => /api/({anyone}/)*tom
  'GET /api/users/?name=/^A.*\\^$/': 'users/example', // => /api/users/?name=A{.*}^
  'POST /api/users': (req, res) => { // 自定义函数 API参考 express
    if (req.body.name === 'tom') {
      res.status(200).send({ message: 'Create user success!' })
    }
  },
}

Config

接口对应的结果可以是 JSON字符串,JSON文件路径,JS文件路径甚至是TXT文件路径,也可以是自定义函数处理返回

如果需要配置文件路径,必须设置dataFile字段(数据文件所在的目录), 文件路径写成相对路径即可

// index.js
let Mock = require('cf-mock-server')

let app = new Mock({
  config: {
    dataFile: './mockData',
    api: {
      'GET /api/users/all': '[{"name":"tom"},{"name":"jerry"}]',
      'GET /api/users/?name=tom': 'users/tom.json',
      'GET /api/users/?name=/^A.*\\^$/': 'users/tom.js',
      'GET /api/users/?name=*': 'users/tom.txt',
      'POST /api/users': (req, res) => {
        if (req.body.name === 'tom') {
          res.status(200).send({ message: 'Create user success!' })
        }
      },
    }
  },
  watch: true,
})

app.run()

CLI

mock -c config.js

// Output
[MOCK] server started on port 8008

// Usage
➜  ~ curl http://localhost:8008/api/users/\?name\=A\^
{"name":"jerry","age":18}%

Mock API

通过new Mock()方式可以创建一个Koa服务

new Mock(options)

创建Mock实例,可以通过options传API配置和设置端口

let mock = new Mock({
  config: path.join(__dirname, './config'), // Mock API 配置, Object or file path
  port: 8009, // 服务监听的端口号
})
mock.setPort(port)

设置服务监听的端口

mock.setPort(8009)
mock.setConfig(config)

设置API配置

mock.setConfig(path.join(__dirname, './config'))

mock.setConfig({ api: { 'GET /api/users/all': '[{"name":"tom"},{"name":"jerry"}]' } })
mock.run()

启动服务, 并返回koa实例

mock.run()

LICENSE

MIT