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

mo-serve

v1.1.28

Published

mockServer

Downloads

5

Readme

超级实用的MOCK server 服务

全局模式使用

  1. 全局安装依耐:nodemon
npm install nodemon mo-serve -g
  1. 退出目录,然后在任意目录下
mo-serve
  1. 命令行参数,--port 设置端口 --docs开启文档,如下:
mo-serve --port 8899 --docs
或
mo-serve --p 8899 --d

1、模板

新建任意以.js结尾的文件,系统将自动读取这些文件,模板文件内容为:

module.exports = {
  'get /demo': {
    status: 0,
    message: '退出',
    data: {}
  }
}

2、key:键名的定义:键名为字符串,"{请求方式} {请求路径}",如:"get /api/test"

批量定义请用“,”隔开,如:"get /api/test,get /api/test2,get /api/test3" 以下将会成功模拟出三个接口!!!

module.exports = {
  "get /api/test,get /api/test2,get /api/test3": {
    status: 0,
    message: '退出',
    data: {}
  }
}

3、value:值为对象或或函数

module.exports = {
  "get /dome": {
    status: 0,
    message: '退出',
    data: {}
  },
  //或
  "get /dome2": (method,path,key)=>{
    //method为方法:get
    //path为访问路径:get/dome2
    //key为完整的键名:get /dome2
    return {
      status: 0,
      message: '退出',
      data: {}
    }
  }
}

4、sleep:睡眠,接口延时返回功能

以下代码,接口将3秒后返回结果

module.exports = {
  "get /dome": {
    sleep: 3000,
    status: 0,
    message: '退出',
    data: {}
  }
}

4、filter或handle:数据过滤或数据处理,对返回的值预先处理,filter或handle可以为异步函数

module.exports = {
  'get /demo': {
    //支持额外的数据过滤函数,有三个参数[req:请求体,res:响应体,data:对应的对象]
    filter({ originalUrl = '',...q }, res, resData) {
      if (originalUrl.includes('/2')) {
        //返回数据为:id===2的数组
        return {
          ...resData,
          data:resData.data.filter( item => item.id===2 )
        }
      }
      return resData
    },
    status: 0,
    message: '',
    data: {}
  },
  'get /demo2': {
    //filter或handle 可以是异步函数
    async filter({ originalUrl = '',...q }, res, resData) {
      let sleep=()=>new Promise(resolve => {
        setTimeout(()=>{
          resolve()
        },3000)
      })
      await sleep()
      if (originalUrl.includes('/2')) {
        //返回数据为:id===2的数组
        return {
          ...resData,
          data:resData.data.filter( item => item.id===2 )
        }
      }
      return resData
    },
    status: 0,
    message: '',
    data: {}
  },
}

5、支持mock语法:常用有 "@id","@name","@cname","@image","@time","@data","@address"...

更多详情 mockjs官网

module.exports = {
  'get /demo': {
    status: 0,
    message: '',
    data: {
      "id":"@id", //随机id
      "role|1":["admin","user"], //数组中随机一个值
      "userLisr|10":[{
        "id|+1":1 // 累加
      }] //随机10条数据
    }
  }
}

5、路由规则,自动识别 /*

注意:能自动识别常用的6种格式:

  • 最后面为 /*,如:/demo/*,
  • 倒数第二级 /*,如:/demo/*/test,
  • 倒数第三级 /*,如:/demo/*/test/test2
  • 倒数两级都为/*,如:/demo/*/*
  • 倒数三级和最后都为/*,如:/demo/*/n/*
  • 倒数三级都为/*,如:/demo/*//

如果超出以上范围:请在请求request.query添加routerRule,告诉系统将匹配的键,例如: 例如:http://127.0.0.1:8022/demo/a/b?routerRule=/demo/:id/:name 或者:http://127.0.0.1:8022/demo/a/b?routerRule=/demo// 或者:http://127.0.0.1:8022/demo/a/b?routerRule=/demo/${id}/${name}

module.exports = {
  //自动匹配
  'get /demo/*/*': {
    status: 0,
    message: '',
    data: {
      "id":"@id", //随机id
    }
  },
  // 带:的路由,无法识别,需要通过request.query.routerRule='get_demo_:id_:name',告诉系统
  // 发出请,应该为:api.get('/demo/11/likuan?routerRule=get_demo_:id_:name')
  'get /demo/:id/:name': {
    status: 0,
    message: '',
    data: {
      "id":"@id", //随机id
    }
  }
}

6、支持将某个接口设为代理

module.exports = {
  'get /sansi/api/getuserinfo': {
    //原本返回的数据
    status: 0,
    message: '',
    // 现在想代理到另一台机器上的同一路径的资源,只要加上proxy:'127.0.0.1:3436'
    // 如果代理,再访问此地址:127.0.0.1:8022/db/get/sansi/api/getuserinfo,
    // 将呈现 127.0.0.1:3436/sansi/api/getuserinfo 的数据
    proxy:'127.0.0.1:3436',
  },

}

最后

祝你使用愉快!有任何问题请联系我!