mock-service-cli 3.3.3 → 3.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ### 简介
11
11
 
12
- 内置 **Static Server**、**Mock Server**、**SPA Web Server**、Http?s request proxy 等功能集。
12
+ 内置 **Static Server**、**Mock Server**、**SPA Server**、Http?s request proxy 等功能集。
13
13
 
14
14
  **简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock 工具之一。
15
15
 
@@ -76,22 +76,24 @@ This will install `mock-service-cli` globally so that it may be run from the com
76
76
 
77
77
  `[path]` defaults to `./mock` .
78
78
 
79
- | Command | Description | Defaults |
80
- | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
81
- | `-h` or `--help` | Print this list and exit. | |
82
- | `-p` or `--port` | Mock server port to use. Use `-p 8090` to look for an open port, starting at 8090. | 8090 |
83
- | `-d` | Specify mock directory | ./mock |
84
- | `-f` | Specify mock file | |
85
- | `-s` or `--silent` | Suppress log messages from output | |
86
- | `-v` or `--version` | Print the version and exit. | |
87
- | `-S` or `--socket-server` | Start socket server which used to save api response data for future mock. | false |
88
- | `-a` or `--api-stat` | Whether print api url and file path info or not, default false. | false |
89
- | `-l` or `--log` | Whether record operation info by write file, default false. | false |
90
- | `-o` or `--cors-origin` | Allow origin by cors, list separated by commas, must not be \* when withCredential is true .If specified, cors-headers will be `Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token` | \* |
91
- | `-O` or `--proxy-options` | Optionally provide http request proxy options list, support accept js file、json file、cli arguments string. Examples: `-O '/api/apaas\|http://192.168.0.105:60700,/api/permission\| http://192.168.0.105:60700'`. Must start web server to use proxy for http request test under browser console or api test tools. | - |
92
- | `-P` or `--web-port` | Web server port to use. Use `-P 9090` to look for an open port, starting at 9090 | 9090 |
93
- | `-b` or `--web-baseurl` | Specify public path of the SPA web server, can be base address of SPA route. | - |
94
- | `-R` or `--static-server` | Specify directory of the Static Web Server. | - |
79
+ | Command | Description | Defaults |
80
+ | -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
81
+ | `-h` or `--help` | Print this list and exit. | |
82
+ | `-p` or `--port` | Mock server port to use. Use `-p 8090` to look for an open port, starting at 8090. | 8090 |
83
+ | `-d` | Specify mock directory | ./mock |
84
+ | `-f` | Specify mock file | |
85
+ | `-s` or `--silent` | Suppress log messages from output | |
86
+ | `-v` or `--version` | Print the version and exit. | |
87
+ | `-S` or `--socket-server` | Start socket server which used to save api response data for future mock. | false |
88
+ | `-a` or `--api-stat` | Whether print api url and file path info or not, default false. | false |
89
+ | `-t` or `--track` | Whether record operation info by write file, default false. | false |
90
+ | `-o` or `--cors-origin` | Allow origin by cors, list separated by commas, must not be \* when withCredential is true .If specified, cors-headers will be `Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token` | \* |
91
+ | `-O` or `--proxy-options` | Optionally provide http request proxy options list, support accept js file、json file、cli arguments string. Examples: `-O '/api/apaas\|http://192.168.0.105:60700,/api/permission\| http://192.168.0.105:60700'`. Must start web server to use proxy for http request test under browser console or api test tools. | - |
92
+ | `-H` or `--cors-headers` | When start Mock Server, optionally provide CORS headers list separated by commas. default . | \* |
93
+ | `-A` or `--append-headers` | When start Mock/Web/Static Server, optionally add response headers list separated by commas. For example: X-Frame-options=DENY,content-security-policy=frame-ancestors. | - |
94
+ | `-P` or `--web-port` | Web server port to use. Use `-P 9090` to look for an open port, starting at 9090. | 9090 |
95
+ | `-b` or `--web-baseurl` | Specify public path of the SPA web server, can be base address of SPA route. | - |
96
+ | `-R` or `--static-server` | Specify directory of the Static Web Server. | - |
95
97
 
96
98
  ## Example
97
99
 
@@ -19,30 +19,33 @@ if (argv.h || argv.help) {
19
19
  '',
20
20
  'PS: Mock File仅支持commonjs规范的js、cjs文件,不支持ES Module',
21
21
  'options:',
22
- ' -p --port Mock server port to use. If 0, look for open port. [8090]',
23
- ' -d Specify mock directory, default [./mock] directory,',
24
- ' Also watch js files changes and support hot reload',
25
- ' -f --file Specify the input data source, support javascript ',
26
- ' The priority is over the directory watches',
22
+ ' -p --port Mock server port to use. If 0, look for open port. [8090]',
23
+ ' -d Specify mock directory, default [./mock] directory,',
24
+ ' Also watch js files changes and support hot reload',
25
+ ' -f --file Specify the input data source, support javascript ',
26
+ ' The priority is over the directory watches',
27
27
  ' -s --silent Suppress log messages from output',
28
- ' -S --socket-server Whether start socket server or not, default false.',
29
- ' -a --api-stat Whether print api url and file path or not, default false.',
30
- ' -l --log Whether record operation info by write file, default false.',
31
- ' -o --cors-origin Allow origin by cors, list separated by commas, must not be * when withCredential is true',
32
- ' .If specified, cors-headers will be "Authorization,Content-Type,Accept,Origin,User-Agent,DNT',
33
- ' ,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token", default *.',
34
- ' -H --cors-headers Optionally provide CORS headers list separated by commas. default *',
28
+ ' -S --socket-server Whether start socket server or not, default false.',
29
+ ' -a --api-stat Whether print api url and file path or not, default false.',
30
+ ' -t --track Whether record operation info by write file, default false.',
31
+ ' -o --cors-origin Allow origin by cors, list separated by commas, must not be * when withCredential is true',
32
+ ' .If specified, cors-headers will be "Authorization,Content-Type,Accept,Origin,User-Agent,DNT',
33
+ ' ,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token", default *.',
34
+ ' -H --cors-headers When start Mock Server, optionally provide CORS headers list separated by commas. default *',
35
+ ' -A --append-headers When start Mock/Web/Static Server, optionally add response headers list separated by commas.',
36
+ ' For example: X-Frame-options=DENY,content-security-policy=frame-ancestors',
35
37
  '',
36
- ' -O --proxy-options Optionally provide proxy options list separated by commas.',
37
- ' -r --rewrite rewrite http or https request url prefix of proxy, default false.',
38
- ' -P --web-port Web server port to use. If 0, look for open port. [9090]',
39
- ' -D --web-dir Enable web server, specify web directory, default [./dist] directory',
40
- ' -b --web-baseurl Specify website public path when enabled web server.',
38
+ ' -O --proxy-options Optionally provide proxy options list separated by commas.',
39
+ ' -r --rewrite rewrite http or https request url prefix of proxy, default false.',
41
40
  '',
42
- ' -R --static-server Specify static resource file directory when enabled static server.',
41
+ ' -D --web-dir Enable web server, specify web directory, default [./dist] directory',
42
+ ' -P --web-port Web server port to use. If 0, look for open port. [9090]',
43
+ ' -b --web-baseurl Specify website public path when enabled web server.',
43
44
  '',
44
- ' -h --help Print this list and exit.',
45
- ' -v --version Print the version and exit.'
45
+ ' -R --static-server Specify static resource file directory when enabled static server.',
46
+ '',
47
+ ' -h --help Print this list and exit.',
48
+ ' -v --version Print the version and exit.'
46
49
  ].join('\n')
47
50
  );
48
51
  process.exit();
@@ -67,7 +67,7 @@ const genMockFiles = async function ({ url: apiUrl, method, data: resJsonData, d
67
67
  return;
68
68
  }
69
69
  apiUrl = apiUrl.trim().split('?')[0];
70
- const LOGGER = logFile(processArgv.l || processArgv.log, mockDir);
70
+ const LOGGER = logFile(processArgv.t || processArgv.track, mockDir);
71
71
  const normalizeApiUrl = filePath2ApiUrl(path.normalize(apiUrl));
72
72
  const mockListFilePath = path.resolve(process.cwd(), `${mockDir}/mock-list.json`);
73
73
  const mockFilePath = path.resolve(process.cwd(), `${mockDir}/${encodeURIComponent(path.normalize(apiUrl))}.json`);
package/lib/mockServer.js CHANGED
@@ -93,6 +93,20 @@ function init() {
93
93
  */
94
94
  function crossDomain() {
95
95
  return (req, res, next) => {
96
+ if (typeof argv.A === 'string') {
97
+ const resHeaders = {};
98
+ argv.A.split(/\s*,\s*/).forEach(function (h) {
99
+ const [key, value] = h.split('=');
100
+ resHeaders[key] = value;
101
+ }, this);
102
+ // 添加自定义响应头
103
+ for (const key in resHeaders) {
104
+ if (resHeaders.hasOwnProperty(key)) {
105
+ res.header(key, resHeaders[key]);
106
+ }
107
+ }
108
+ }
109
+
96
110
  // 设置withCredentials: true时,需设置以下两项
97
111
  res.header('Access-Control-Allow-Credentials', true);
98
112
  // withCredentials, must be specified value instead of *
@@ -233,6 +247,23 @@ function startServer() {
233
247
  console.warn(colors.yellow('\nEnable web proxy Failed:', error));
234
248
  }
235
249
  }
250
+ // 添加自定义响应头
251
+ if (typeof argv.A === 'string') {
252
+ webApp.use((req, res, next) => {
253
+ const resHeaders = {};
254
+ argv.A.split(/\s*,\s*/).forEach(function (h) {
255
+ const [key, value] = h.split('=');
256
+ resHeaders[key] = value;
257
+ }, this);
258
+ // 添加自定义响应头
259
+ for (const key in resHeaders) {
260
+ if (resHeaders.hasOwnProperty(key)) {
261
+ res.header(key, resHeaders[key]);
262
+ }
263
+ }
264
+ next();
265
+ });
266
+ }
236
267
  // console.log(process.env.WEB_PUBLIC_PATH, process.env.WEB_ROOT);
237
268
  if (process.env.WEB_PUBLIC_PATH) {
238
269
  webPublicPath = process.env.WEB_PUBLIC_PATH;
@@ -2,7 +2,7 @@
2
2
  * Static Server
3
3
  * @Date: 2023-12-03 18:02:24
4
4
  * @LastEditors: chendq
5
- * @LastEditTime: 2025-06-15 22:16:36
5
+ * @LastEditTime: 2025-07-29 19:20:21
6
6
  * @Author : chendq
7
7
  */
8
8
  const express = require('express'), // 引入express
@@ -12,6 +12,9 @@ const express = require('express'), // 引入express
12
12
  const { dateFormat, logger } = require('./utils');
13
13
  const ifaces = os.networkInterfaces();
14
14
  const log = logger(process.env.SILENT);
15
+ const argv = JSON.parse(process.env.ARGV);
16
+
17
+ const resHeaders = {};
15
18
 
16
19
  if (!process.env.PORT) {
17
20
  portfinder.basePort = 8090;
@@ -26,12 +29,36 @@ if (!process.env.PORT) {
26
29
  startServer();
27
30
  }
28
31
 
32
+ /**
33
+ * @description: 添加自定义响应头
34
+ * @param {*}
35
+ * @return {*}
36
+ */
37
+ function appendResHeaders() {
38
+ return (req, res, next) => {
39
+ for (const key in resHeaders) {
40
+ if (resHeaders.hasOwnProperty(key)) {
41
+ res.header(key, resHeaders[key]);
42
+ }
43
+ }
44
+ next();
45
+ };
46
+ }
47
+
29
48
  /**
30
49
  * Start server
31
50
  */
32
51
  function startServer() {
33
52
  const app = express();
34
53
 
54
+ if (typeof argv.A === 'string') {
55
+ argv.A.split(/\s*,\s*/).forEach(function (h) {
56
+ const [key, value] = h.split('=');
57
+ resHeaders[key] = value;
58
+ }, this);
59
+ app.use(appendResHeaders()); // 添加响应头
60
+ }
61
+
35
62
  app.use(express.static(process.env.STATIC_DIRECTORY)); // 将dist目录下所有文件作为静态文件来管理
36
63
 
37
64
  app.listen(Number.parseInt(process.env.PORT, 10), () => {
@@ -67,11 +94,11 @@ if (process.platform === 'win32') {
67
94
  }
68
95
 
69
96
  process.on('SIGINT', function () {
70
- log.info(colors.red('mock-server process stopped.'));
97
+ log.info(colors.red('static-server process stopped.'));
71
98
  process.exit();
72
99
  });
73
100
 
74
101
  process.on('SIGTERM', function () {
75
- log.info(colors.red('mock-server process stopped.'));
102
+ log.info(colors.red('static-server process stopped.'));
76
103
  process.exit();
77
104
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mock-service-cli",
3
- "version": "3.3.3",
4
- "description": "🦅 Local mock server, Static server, SPA server, Http?s request proxy",
3
+ "version": "3.3.5",
4
+ "description": "🦅 Local Mock/Static/SPA server, Http?s request proxy",
5
5
  "main": "./lib/mockServer.js",
6
6
  "bin": {
7
7
  "mock-service-cli": "bin/mock-service-cli"
@@ -29,10 +29,11 @@
29
29
  },
30
30
  "author": "chendq <deqiaochen@gmail.com>",
31
31
  "keywords": [
32
+ "Mock",
32
33
  "Static Server",
33
34
  "Web Server",
34
35
  "Mock Server CLI",
35
- "MockServer",
36
+ "SPA Server",
36
37
  "mock-service-cli",
37
38
  "Socket server"
38
39
  ],