Skip to content

Repository files navigation

jmock

Downloads Version License

jmock β€” A simple and easy-to-use CLI HTTP server with Mock, Proxy, and static file serving.

Open source at: https://github.com/Yakima-Teng/jmock

Features

  • ⚑ Ready out of the box: Start an HTTP server with a single command, with static file serving
  • 🎯 Data mocking: Powerful mock data generation based on Mock.js
  • πŸ”€ Request proxy: Built-in proxy to easily solve CORS issues
  • πŸ“„ Auto directory listing: Browse static files with directory index display
  • πŸ”’ HTTPS support: Configurable HTTPS certificates for secure access

Tech Stack

  • βš™οΈ Runtime: Node.js >= 22.18
  • πŸ“˜ Language: TypeScript / ES Module
  • 🧰 Tools: ESLint, Prettier, markdownlint-cli2
  • πŸ”§ Dependencies: http-proxy, mockjs, union, chalk

Requirements

  • πŸ“¦ Node.js >= 22.18

Installation

Global Install (Recommended)

npm install --global jmock

After global installation, the jmock command is available from any directory.

Run on Demand

Use npx to run without installing in advance:

npx jmock [path] [options]

Install as a Project Dependency

npm install jmock

Usage

jmock [path] [options]

[path] defaults to ./public (if it exists), otherwise ./.

After starting, visit http://localhost:8080 to view the server.

Options

Option Description
-p <port> Specify port number, e.g. jmock -p 8082
--cors Enable CORS via Access-Control-Allow-Origin header
-o <path> Auto-open a specific path after startup
--config Generate default config file jmock.config.mjs

Data Mocking

Create a jmock.config.mjs file with the mockTable field:

export default {
  mockTable: {
    // Supports req, query, body, method, Mock parameters
    "/api/hello": ({ req, query, body, method, Mock }) => {
      return {
        code: 200,
        data: {
          method,
          query,
          body,
          data: Mock.mock({
            "list|1-10": [{ "id|+1": 1 }],
          }),
        },
        message: "success",
      };
    },
    // Supports async/await
    "/api/world": async ({ query, body, method, Mock }) => {
      await new Promise((resolve) => setTimeout(resolve, 300));
      if (method === "GET") {
        return {
          code: 200,
          data: {
            method,
            query,
            body,
            data: Mock.Random.paragraph(3, 7),
          },
          message: "success",
        };
      }
      return {
        code: 200,
        data: { method, query, body, data: Date.now() },
        message: "it's not a GET request.",
      };
    },
  },
};

Start the server and request /api/world?c=1&d=hello:

fetch("/api/world?c=1&d=hello", {
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json",
  },
  method: "POST",
  body: JSON.stringify({ a: 11, b: 22 }),
})
  .then((res) => res.json())
  .then((res) => console.log(res.data));

Example response:

{
  "code": 200,
  "data": {
    "method": "POST",
    "query": { "c": "1", "d": "hello" },
    "body": { "a": 11, "b": 22 },
    "data": 1706670742590
  },
  "message": "it's not a GET request."
}

Request Proxy

Add the proxyTable field to jmock.config.mjs:

export default {
  proxyTable: {
    // Proxy /baidu-search?wd=keyword to https://www.baidu.com/s?wd=keyword
    "/baidu-search": {
      target: "https://www.baidu.com",
      changeOrigin: true,
      pathRewrite(path) {
        return path.replace("/baidu-search", "/s");
      },
    },
    // Proxy /search?q=keyword to https://cn.bing.com/search?q=keyword
    "/search": {
      target: "https://cn.bing.com",
      changeOrigin: true,
      cookieDomainRewrite: "",
    },
  },
};

Generate Config File

jmock --config

Generates a jmock.config.mjs file in the current working directory with detailed example code.

Acknowledgments

jmock is built on top of these projects:

License

πŸ“„ Apache License 2.0

About

🀠 command-line http server for mocking data, proxying requests and serving static files

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages