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

dynamic-mock-express

v1.1.2

Published

A dynamic mock tools to response http request

Downloads

19

Readme

dynamic-mock-express

A middleWare based on express to handle http request when real server is unfinished

Build Status Coverage Status

feature

1.Suport Restful API friendly
2.Flexible configuration to support custom filtering URL
3.Could accept pathParams, query, body in different request methods.
4.No cache, config‘s change does not need to be restarted
5.Suport dynamic, responsive result
6.Support function nesting.
7.Support custom response.

include

npm i dynamic-mock-express -D

express server

const app = express();
const mock = require("dynamic-mock-express");
app.use(
  mock({
    mockDir: path.resolve(__dirname, "../mock")
  })
);

webpack-dev-server, such as vue-cli or create-react-app

const mock = require("dynamic-mock-express");
new WebpackDevServer(compiler, {
  ...
  setup: (app) => {
    app.use(
        mock({
          mockDir: path.resolve(__dirname, "../mock"),
          entry: "index.js" // default is index.js
        })
    );
  }
})

usage

mock/index.js

module.exports = {
  needMock: true,
  prefix: "api",
  tip: true,
  ignore: url => {// could filter some request url by return true
  
  },
  routes: {
    "GET:a/b": require("./mock_1"),
    "GET:a/b/:id": (data) => { // suport return a dynamic json
      return {
        data: "mock_2",
        params: data.params,
      };
    },
    "GET:b/:id/:code": require("./mock_3"),
    "POST:a/b": require("./mock_4"),
    "POST:b/c": {      // suport return a json directly
       a:1
    },
    "POST:a/c": (req, res, next) => { //support custom response when res as arguement.
      res.writeHead(200, {"Content-Type": "application/json; charset=UTF-8"});
      res.write(JSON.stringify({name: 'lyl'}));
      res.end();
    },
    "POST:a/b/c": data => {
      return {
        status: true,
        params: data.params,
        query: data.query,
        body: data.body
      };
    },
    "DELETE:a/b/:id": (data) => {
      return {
        id: data.params.id
      };
    },
    "DELETE:a/b/c/:id":  { // support function nesting.
       a: {
         b: 1,
         c: (data) => {
           return {
             id: data.params.id
           }
         }
       }
    }
  }
};

example

If http request is ${host}/api/a/b/10, dynamic -mock-express returns the following results based on the above configuration

{
   data: "mock_2",
   params: {
     id: "10"
   }
}

responsive result

mock/index.js

module.exports = {
  needMock: true,
  prefix: "api",
  storePath: Path.reslove(__dirname, "store"), //must a absolute path
  tip: true,
  routes: {
    "GET:a/b/:id": ({store, params}) => {
       return store.data.find(item => {return item.id == params.id});
    },
    "POST:a/b": ({store, body}) => {
       store.data.find(item => {
          return item.id == body.id
       }).name = body.name;
       return {
          status: true
       };
    }
  }
}

mock/store.js

module.exports = {
    data: [
      {
	id: 10,
        name: "zhangsan"  
      },
      {
	id: 11,
        name: "lisi"  
      }
    ]
}

example

// pseudocode
get("api/a/b/10").then(res => {
  console.log(res) // {id: 10, name: "zhangsan"}
  post("api/a/b")
     .send({id: 10, name: "wangwu"})
     .then(res => {
        get("api/a/b/10").then(res =>{
          console.log(res) // {id: 10, name: "wangwu"}
        })
     })
})

config params

|paramsName|means|type|default| |-|-|-|-| |needMock|allow mock|Boolean|true| |delay|delay of response|Number|0| |storePath|store path|String|""| |prefix|prefix of request url|String|""| |ignore|filter some request|Function[return a boolean]|(url) => {return false}| |tip|open warn when not match url |Boolean|true|