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
- β‘ 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
- βοΈ Runtime: Node.js >= 22.18
- π Language: TypeScript / ES Module
- π§° Tools: ESLint, Prettier, markdownlint-cli2
- π§ Dependencies: http-proxy, mockjs, union, chalk
- π¦ Node.js >= 22.18
npm install --global jmockAfter global installation, the jmock command is available from any directory.
Use npx to run without installing in advance:
npx jmock [path] [options]npm install jmockjmock [path] [options][path] defaults to ./public (if it exists), otherwise ./.
After starting, visit http://localhost:8080 to view the server.
| 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 |
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."
}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: "",
},
},
};jmock --configGenerates a jmock.config.mjs file in the current working directory with detailed example code.
jmock is built on top of these projects:
π Apache License 2.0