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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@keqingrong/mock-server

v0.0.1

Published

Mock server

Downloads

1

Readme

@keqingrong/mock-server

基于 Koa 实现的 Mock 服务

安装

# npm
npm install @keqingrong/mock-server

# yarn
yarn add @keqingrong/mock-server

使用

./
├── mock
|  ├── example/
|  |  ├── form-data.js
|  |  ├── get.js
|  |  ├── json.js
|  |  └── x-www-from-urlencoded.js
|  └── loginst/
|     └── authStatus.js
└── package.json
$ mock-server --watch --port 8090 --api-base-path ./mock

$ open http://localhost:8090/example/get
$ mock-server --help

  Mock server

  Usage
    $ mock-server

  Options
    --port, -p  Mock 服务的启动端口
    --config, -c  Mock 服务的启动配置
    --base-path  Mock 服务 API 目录
    --pretty  是否对 JSON Response 美化
    --watch, -w  是否监控 API 目录文件变化
    --legacy  是否需要兼容 express-mockjs 写法

  Examples
    $ mock-server --port 8090
    $ mock-server --base-path ./mock/apis
    $ mock-server --config ./config/mock.config.js
    $ mock-server --watch --base-path ./mock/apis

Mock 编写

假设当前项目为 project,在 mock 文件夹下存放 Mock 数据。

用法一:定义 JSON(默认支持 mockjs 语法)

{
  "code": "000000",
  "data": [],
  "msg": "处理成功"
}

文件路径:project/mock/demo/qux.json,会自动创建路由为 /demo/qux 的接口,支持 GET/POST/JSONP 等方法。

注意:如果配置 legacy,会增加对 express-mockjs 的兼容处理。当 JSON 文件命名为 data.json 时,会尝试解析其中的 JSDoc,如 @url /demo/foo 作为路由地址,如果解析失败则回退成父级目录路径,如 project/mock/demo/foo/data.json,会创建路由 /demo/foo

用法二:定义 CommonJS 模块(默认支持 mockjs 语法),要求模块默认导出的方法调用后返回对象

module.exports = () => ({
  foo: 'bar',
  list: [1, 2, 3, 4, 5]
});

文件路径:project/mock/demo/foo.js,会自动创建路由为 /demo/foo 的接口,支持 GET/POST/JSONP 等方法。

用法三:定义 Koa 中间件,要求模块默认导出的方法调用后返回的对象中包含 middleware 函数

module.exports = ({ mock }) => ({
  async middleware(ctx, next) {
    ctx.body = {
      foo: 'bar',
      list: [1, 2, 3, 4, 5]
    };
  }
});

文件路径:project/mock/demo/foo.js,会自动创建路由为 /demo/foo 的接口,支持 GET/POST 等方法。

用法四:定义 Koa 中间件、请求方法和路径(忽略文件路径),允许同时定义多个

module.exports = ({ mock }) => ({
  method: 'get',
  path: '/demo/bar',
  async middleware(ctx, next) {
    ctx.body = {
      foo: 'bar',
      list: [1, 2, 3, 4, 5]
    };
  }
});

创建仅支持 GET 方法,路由为 /demo/bar 的接口。

module.exports = ({ mock }) => ([
  {
    method: 'get',
    path: '/example/multiple/foo',
    async middleware(ctx, next) {
      ctx.body = {
        foo: 'foo',
        list: [1, 2, 3, 4, 5]
      };
    }
  },
  {
    method: 'get',
    path: '/example/multiple/bar',
    async middleware(ctx, next) {
      ctx.body = {
        foo: 'bar',
        list: [1, 2, 3, 4, 5]
      };
    }
  },
  {
    method: 'get',
    path: '/example/multiple/jsonp',
    async middleware(ctx, next) {
      ctx.jsonp = {
        foo: 'bar',
        list: [1, 2, 3, 4, 5]
      };
    }
  }
]);

在一个模块中同时定义多个接口路由(每个子路由都必须定义 path)。

用法五:完全自定义 Koa 路由(不能返回对象,否则和用法二冲突)

module.exports = ({ router, mock }) => {
  router.get('/demo/baz', (ctx, next) => {
    ctx.body = 'Hello World!';
  });
};

TODO

  • [x] 支持 HEAD/GET/POST
  • [x] 支持 JSON/JSONP
  • [x] 自动扫描目录 JS 和 JSON 生成接口
  • [x] 支持 CORS
  • [x] 支持图片上传
  • [ ] 支持分页
  • [ ] 支持视图
  • [ ] 端口占用,自动切换端口
  • [x] 显示局域网地址
  • [ ] 支持集成到 webpack-dev-server(本项目基于 koa,而 webpack-dev-server 基于 express)
  • [x] 支持文件修改后重新加载路由
  • [x] 兼容 express-mockjs

相关链接