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

mock-express-server

v0.9.5

Published

a mock server with node express

Downloads

14

Readme

Mock 数据服务工具

基于 expressswagger config 的 Mock 数据服务工具, 为对接接口的过程提供便利

npm version NPM Downloads Build Status MIT

介绍 :jack_o_lantern:

根据 swagger 配置, 利用 express 动态创建生成 router(包括 route path、route handler),在 route handler 中校验 req 中参数合理性,与利用faker.js mock 数据返回 res

基本原理:

利用 swagger 的 path 生成对应的路由处理函数、path 下的 parameters 用来校验接口请求的参数、path 下面的 responses 用来 mock 接口返回接口参数,如果传参有误的话,则会返回具体错误详情。

目前只支持swagger 2.0 规范配置 JSON 数据格式

快速开始

安装

via yarn

yarn global add mock-express-server

or npm

npm install -g mock-express-server

使用

  • 全局命令方式

Tip: mock-express-server 包的全局命令为:mock-server, 使用mock-server --help命令可查看命令使用说明,如图:

执行 start 命令即可启动 node 服务,如图:

执行 mock-server help start 查看更多选项使用,如图:

  • 配置文件方式

在项目根目录下添加mock.config.js 文件,根据自己项目情况在 mock.config.js 中配置好 url 或者 localPath, 启动命令即可 mock-server start 命令即可(可结合 package.json, 配置在 script 中)

mock.config.js 配置项

无论是哪种方式使用,都是覆盖 mock-server 的默认值的,其默认值如下:

const config = {
  port: 9009, // 启动端口
  copy: false, // 是否开始自动复制
  localPath: '', // 1. 本机绝对路径;2. 相对项目根目录路径(拼接`process.cwd()`)路径;(若是文件夹路径,则会遍历该文件夹下的所有文件 swagger 配置文件), 例如:'local/api-docs.json'
  tag: '', // 对应swagger config 的tags,空的话,则选择全部tags的path, 配置的话经过筛选后,只启动该tag下面的接口, 例如:/pet'
  url: 'https://petstore.swagger.io/v2/swagger.json', // swagger config 接口路径,例如:https://petstore.swagger.io/v2/swagger.json, 如果同时配置了url与localPath,合并两者,若有冲突,以url配置为止
  openLocalRedis: false, // 是否开始redis存储swagger 配置(一般用于开发阶段)
  openValidParams: false, // 是否检验请求参数类型合法,false,则不检验, 直接返回响应数据
  codeMap: {
    success: 20000, // 成功逻辑code
    unlogin: 40001, // 没有登录
    parameterError: 40003, //参数错误
  },
};

配置文件属性

优先级:命令行输入 > 用户自定义配置文件 > 默认配置文件; Object.assign(config, userConfig, option);

| name | type | default | description | | :-------------: | :-----: | :-------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | | port | number | 9009 | mock server 端口号 | | localPath | string | '' | 1. 本机绝对路径;2. 相对项目根目录路径(拼接process.cwd())路径;(若是文件夹路径,则会遍历该文件夹下的所有文件 swagger 配置文件) | | tag | string | '' | 对应 swagger config 的 tags,空的话,则选择全部 tags 的 path, 配置的话经过筛选后,只启动该 tag 下面的接口, 例如:/pet' | | url | string | '' | swagger config 数据源 url,目前只支持 Swagger 2.0。如 https://petstore.swagger.io/v2/swagger.json, 如果同时配置了 url 与 localPath, 合并两者,若有冲突,以 url 配置为止 | | openLocalRedis | boolean | false | 是否开启 redis 存储 swagger 配置(一般用于开发调试阶段) | | openValidParams | boolean | false | 是否检验请求参数类型合法,为 false,则不检验, 直接返回响应数据 | | codeMap | object | { success: 20000, unlogin: 40001, parameterError: 40003 } | 成功,错误等状态码 Code 映射 |

子属性

  • codeMap

| name | type | default | description | | :------------: | :----: | :-----: | :-----------: | | success | number | 20000 | 请求正确 code | | unlogin | number | 40001 | 登录失效 code | | parameterError | number | 40003 | 传参错误 code |