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

mock-dev-server

v1.0.2

Published

A simple server for mockjs

Readme

mock-dev-server

一个简易的Mock服务器

安装

# 全局安装,方便使用 mock-dev-server 命令
npm install -g mock-dev-server

# 局部安装
npm install --save-dev mock-dev-server

如何使用

  1. 在项目根目录建一个 mock 文件夹
  2. mock 文件夹里新建 *.json 文件,在 json 文件里面编辑 mock 数据(示例
  3. 路由说明:请求的url是根据 mock 文件夹下的层次结构来生成的(就是 json 文件相对于 mock 文件夹的路径,并去掉 .json 扩展名),如下示项目结构示例将会生成以下路由:
  /a
  /sub/b
  /sub/c

  // 如果设置的基本路径, 将会在路由最前面加上基本路径,如 `mock-dev-server -b /api`,将会生成以下路由:

  /api/a
  /api/sub/b
  /api/sub/c
  1. 配置 Mock 数据的 json 文件说明:API与 Mockjs官网api一致,使用 Mock.mock() 方法返回随机数据
  2. 运行命令 mock-dev-server ,即可在 http://localhost:3000/* 请求到api,支持所有的请求类型,如果要动态改变 mock 数据,则需要在命令行添加 -w 参数

通过node脚本启动示例

const mockServer = require('mock-dev-server')
mockServer({
  mock = 'mock',
  watch = false,
  port = 3000,
  base = '/'
}, {
  status: 200,
  text: 'success'
})

// mockServer({
//   port = 3000
// }, function() {
//   return {
//     status: 200
//   }
// })

(v1.0.0新增)mockServer方法的第一个参数是一个对象,与命令行参数类似,第二个参数(可选)表示通用数据,可以是一个对象或一个函数(返回一个对象),所有的请求都会返回这些通用数据

示例

  1. 目录结构
├── ...
├── mock
│   ├── a.json
│   └── sub
│       ├── b.json
│       └── c.json
├── node_modules
├── index.js
└── package.json
  1. a.json 示例
{
  "id|+1": 1,
  "name": "@cname",
  "description": "@cparagraph",
  "age|20-50": 20,
  "email": "@email",
  "birthday": "@datetime('yyyy-MM-dd')"
}

postman 请求 http://localhost:3000/a ,得到如下响应数据:

{
  "id": 1,
  "name": "熊强",
  "description": "值月利反十历金细问此活场件收。即何林口属院例直起同政候文管研至龙。治整支料去林用铁严面即总要小。",
  "age": 32,
  "email": "[email protected]",
  "birthday": "2003-07-05"
}
  1. 运行命令
# 启动 mock server
mock-dev-server

# 监听 /mock/**/*.json 文件
mock-dev-server -w

# 配置 mock 数据目录(下面的示例将会在读取根目录下的 data 文件夹)
mock-dev-server -m data

# 配置端口号(默认:3000)
mock-dev-server -p 3000

# 配置请求的基本路径,必须以 / 开始,如 /api
# 如下配置请求url就变成了 http://localhost:3000/api/a
mock-dev-server -b /api

命令行参数

Usage: mock-dev-server [options]

Options:
  -m, --mock   设置mock数据目录                       [string] [default: "mock"]
  -w, --watch  是否监听mock目录文件                                    [boolean]
  -p, --port   Mock Server 端口号                       [number] [default: 3000]
  -b, --base   路由根目录(必须以 / 开始)                 [string] [default: "/"]
  -h, --help   Show help                                               [boolean]

Examples:
  mock-dev-server -m mock --port 3000 -w -b /api