mock-service-cli 2.5.1 → 3.1.0
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/CHANGELOG.md +14 -0
- package/README.md +52 -16
- package/bin/mock-service-cli +73 -24
- package/lib/mockServer.js +84 -5
- package/lib/utils.js +3 -1
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [3.1.0](https://github.com/chandq/mock-service-cli/compare/v3.0.0...v3.1.0) (2022-04-27)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **web-proxy:** proxy配置项支持文件和命令行参数字符串两种 ([ee1ce1f](https://github.com/chandq/mock-service-cli/commit/ee1ce1fbefa5ce9b3c074192d2d9893a4414d346))
|
|
11
|
+
|
|
12
|
+
## [3.0.0](https://github.com/chandq/mock-service-cli/compare/v2.5.1...v3.0.0) (2022-04-27)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* add web server and request proxy ([e5be340](https://github.com/chandq/mock-service-cli/commit/e5be340940042eb05e2e5cb9d2fcc6f8316052d5))
|
|
18
|
+
|
|
5
19
|
### [2.5.1](https://github.com/chandq/mock-service-cli/compare/v2.5.0...v2.5.1) (2022-04-25)
|
|
6
20
|
|
|
7
21
|
## [2.5.0](https://github.com/chandq/mock-service-cli/compare/v2.4.0...v2.5.0) (2022-04-25)
|
package/README.md
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
|
|
11
11
|
### 简介
|
|
12
12
|
|
|
13
|
+
内置 Mock Server、Web Server、Http request proxy 等功能集。
|
|
14
|
+
|
|
13
15
|
**简易轻量**、**B/S 架构**、**0 秒启动**的本地命令行 Mock 服务套件, 支持热更新,对于开发调试 mock 数据很实用,能提高前端开发者的开发效率。支持 `GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE` 等常用请求类型,无需布署后端,可能是本地最好用的 Mock 工具。支持以下常见业务场景:
|
|
14
16
|
|
|
15
17
|
- [x] 无服务端演示项目的数据 Mock
|
|
@@ -45,29 +47,31 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
45
47
|
|
|
46
48
|
`[path]` defaults to `./mock` .
|
|
47
49
|
|
|
48
|
-
| Command | Description
|
|
49
|
-
| ------------------------- |
|
|
50
|
-
| `-h` or `--help` | Print this list and exit.
|
|
51
|
-
| `-p` or `--port` |
|
|
52
|
-
| `-d` | Specify mock directory
|
|
53
|
-
| `-f` | Specify mock file
|
|
54
|
-
| `-s` or `--silent` | Suppress log messages from output
|
|
55
|
-
| `-v` or `--version` | Print the version and exit.
|
|
56
|
-
| `-S` or `--socket-server` | Start socket server which used to save api response data for future mock.
|
|
57
|
-
| `-a` or `--api-stat` | Whether print api url and file path info or not, default false.
|
|
58
|
-
| `-l` or `--log` | Whether record operation info by write file, default false.
|
|
59
|
-
| `-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`
|
|
60
|
-
| `-
|
|
50
|
+
| Command | Description | Defaults |
|
|
51
|
+
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
|
|
52
|
+
| `-h` or `--help` | Print this list and exit. | |
|
|
53
|
+
| `-p` or `--port` | Mock server port to use. Use `-p 8090` to look for an open port, starting at 8090. | 8090 |
|
|
54
|
+
| `-d` | Specify mock directory | ./mock |
|
|
55
|
+
| `-f` | Specify mock file | |
|
|
56
|
+
| `-s` or `--silent` | Suppress log messages from output | |
|
|
57
|
+
| `-v` or `--version` | Print the version and exit. | |
|
|
58
|
+
| `-S` or `--socket-server` | Start socket server which used to save api response data for future mock. | false |
|
|
59
|
+
| `-a` or `--api-stat` | Whether print api url and file path info or not, default false. | false |
|
|
60
|
+
| `-l` or `--log` | Whether record operation info by write file, default false. | false |
|
|
61
|
+
| `-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` | \* |
|
|
62
|
+
| `-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. | - |
|
|
63
|
+
| `-P` or `--web-port` | Web server port to use. Use `-P 9090` to look for an open port, starting at 9090 | 9090 |
|
|
64
|
+
| `-b` or `--web-baseurl` | Specify public path of the static web server, can be base address of SPA route. | - |
|
|
61
65
|
|
|
62
66
|
## Example
|
|
63
67
|
|
|
64
|
-
1. Specify server port.
|
|
68
|
+
1. Specify mock server port.
|
|
65
69
|
|
|
66
70
|
`mock-service-cli -p 8085`
|
|
67
71
|
|
|
68
|
-
2. Enable cors, specify origin and headers.
|
|
72
|
+
2. Enable cors of mock server, specify the origin and headers of http request.
|
|
69
73
|
|
|
70
|
-
`mock-service-cli -o "http://192.168.0.8:38200,http://10.30.30.3" -H "
|
|
74
|
+
`mock-service-cli -o "http://192.168.0.8:38200,http://10.30.30.3" -H "custom-header"`
|
|
71
75
|
|
|
72
76
|
3. Specify mock directory.
|
|
73
77
|
|
|
@@ -81,6 +85,28 @@ This will install `mock-service-cli` globally so that it may be run from the com
|
|
|
81
85
|
|
|
82
86
|
`mock-service-cli -S`
|
|
83
87
|
|
|
88
|
+
6. Start static web server for SPA, and optionally specify public path and port.
|
|
89
|
+
`mock-service-cli -D ../react-best-practice/dist [-b '/redbridge/'] [-P 9090]`
|
|
90
|
+
|
|
91
|
+
7. Enable Proxy base on above item 6. support accept js file、json file、cli arguments string
|
|
92
|
+
|
|
93
|
+
- cli arguments string
|
|
94
|
+
|
|
95
|
+
`mock-service-cli -D ./ -O '/api/apaas|http://192.168.0.105:60700,/api/permission|http://192.168.0.105:60700'`
|
|
96
|
+
|
|
97
|
+
- js file
|
|
98
|
+
|
|
99
|
+
`mock-service-cli -D ./ -O ./proxy.js`
|
|
100
|
+
|
|
101
|
+
proxy.js file content as follows:
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
module.exports = {
|
|
105
|
+
'/api/apaas': 'http://192.168.0.105:60700',
|
|
106
|
+
'/api/permission': 'http://192.168.0.105:60700'
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
84
110
|
### 编写 Mock 文件
|
|
85
111
|
|
|
86
112
|
> mock 请求的类型支持`GET`,`POST`,`PUT`,`DELETE`,`PATCH`,`OPTIONS`,`COPY`,`LINK`,`UNLINK`,`PURGE`
|
|
@@ -113,6 +139,8 @@ module.exports = {
|
|
|
113
139
|
|
|
114
140
|
然后访问 `本地代理地址` + `/mock/api/222/test` (例如`http://127.0.0.1:8090/mock/api/222/test`) 就能得到 { aa: 1, bb: '使用 id 占位符' } 的响应,其他以此类推。
|
|
115
141
|
|
|
142
|
+

|
|
143
|
+
|
|
116
144
|
### 引入 Mock.js
|
|
117
145
|
|
|
118
146
|
[Mock.js](http://mockjs.com/) 是常用的辅助生成模拟数据的三方库,借助他可以提升我们的 mock 数据能力。
|
|
@@ -141,6 +169,14 @@ module.exports = {
|
|
|
141
169
|
};
|
|
142
170
|
```
|
|
143
171
|
|
|
172
|
+
### 启动 Web Server, 在本地像 nginx 一样运行 dist 文件对应的 SPA
|
|
173
|
+
|
|
174
|
+

|
|
175
|
+
|
|
176
|
+
### 启动 Web Server, 启用 Proxy
|
|
177
|
+
|
|
178
|
+

|
|
179
|
+
|
|
144
180
|
### 借助 socket.io 保留接口数据
|
|
145
181
|
|
|
146
182
|
> `save-data` 和 `mock-dir-stat` 事件会自动生成 `mock-list.json` 文件(维护接口`<url, [method]>`的关系映射),方便查阅接口统计信息
|
package/bin/mock-service-cli
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const colors = require('colors/safe'),
|
|
4
4
|
portfinder = require('portfinder'),
|
|
5
|
-
|
|
5
|
+
{ resolve } = require('path'),
|
|
6
6
|
nodemon = require('nodemon');
|
|
7
|
+
const { existsSync } = require('fs-extra');
|
|
7
8
|
const argv = require('minimist')(process.argv.slice(2));
|
|
8
9
|
const { logger } = require('../lib/utils');
|
|
9
10
|
const spawn = require('child_process').spawn;
|
|
@@ -16,20 +17,25 @@ if (argv.h || argv.help) {
|
|
|
16
17
|
'usage: mock-service-cli [options]',
|
|
17
18
|
'',
|
|
18
19
|
'options:',
|
|
19
|
-
' -p --port
|
|
20
|
+
' -p --port Mock server port to use. If 0, look for open port. [8090]',
|
|
20
21
|
' -d Specify mock directory, default [./mock] directory,',
|
|
21
22
|
' Also watch js files changes and support hot reload',
|
|
22
23
|
' -f --file Specify the input data source, support javascript ',
|
|
23
24
|
' The priority is over the directory watches',
|
|
24
|
-
' -s --silent
|
|
25
|
+
' -s --silent Suppress log messages from output',
|
|
25
26
|
' -S --socket-server Whether start socket server or not, default false.',
|
|
26
|
-
' -a --api-stat
|
|
27
|
-
' -l --log Whether
|
|
28
|
-
' -o --cors-origin
|
|
29
|
-
'
|
|
30
|
-
'
|
|
27
|
+
' -a --api-stat Whether print api url and file path or not, default false.',
|
|
28
|
+
' -l --log Whether record operation info by write file, default false.',
|
|
29
|
+
' -o --cors-origin Allow origin by cors, list separated by commas, must not be * when withCredential is true',
|
|
30
|
+
' .If specified, cors-headers will be "Authorization,Content-Type,Accept,Origin,User-Agent,DNT',
|
|
31
|
+
' ,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Requested-With,X-Data-Type,X-Auth-Token,Token", default *.',
|
|
31
32
|
' -H --cors-headers Optionally provide CORS headers list separated by commas. default *',
|
|
32
33
|
'',
|
|
34
|
+
' -O --proxy-options Optionally provide proxy options list separated by commas.',
|
|
35
|
+
' -P --web-port Web server port to use. If 0, look for open port. [9090]',
|
|
36
|
+
' -D --web-dir Enable web server, specify web directory, default [./dist] directory',
|
|
37
|
+
' -b --web-baseurl Specify website public path when enabled web server.',
|
|
38
|
+
'',
|
|
33
39
|
' -h --help Print this list and exit.',
|
|
34
40
|
' -v --version Print the version and exit.'
|
|
35
41
|
].join('\n')
|
|
@@ -53,6 +59,12 @@ if (!argv.s && !argv.silent) {
|
|
|
53
59
|
process.env.SILENT = true;
|
|
54
60
|
log = logger(true);
|
|
55
61
|
}
|
|
62
|
+
// 注入环境变量
|
|
63
|
+
const injectEnvVariable = (envName, envArgs) => {
|
|
64
|
+
if (envArgs) {
|
|
65
|
+
process.env[envName] = envArgs;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
56
68
|
|
|
57
69
|
if (isStartSocketServer) {
|
|
58
70
|
process.env.SOCKET_SERVER = true;
|
|
@@ -63,33 +75,70 @@ if (version) {
|
|
|
63
75
|
process.exit();
|
|
64
76
|
}
|
|
65
77
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
78
|
+
injectEnvVariable('CORS_ORIGIN', argv.o || argv['cors-origin']);
|
|
79
|
+
injectEnvVariable('CORS_HEADERS', argv.H || argv['cors-headers']);
|
|
69
80
|
|
|
70
|
-
|
|
71
|
-
|
|
81
|
+
injectEnvVariable('WEB_PUBLIC_PATH', argv.b || argv['web-baseurl']);
|
|
82
|
+
// injectEnvVariable('PROXY_OPTIONS', argv.O || argv['proxy-options']);
|
|
83
|
+
// 解析Web代理配置项(来源js|config文件或命令行参数字符串)
|
|
84
|
+
const proxyArg = argv.O || argv['proxy-options'];
|
|
85
|
+
if (proxyArg) {
|
|
86
|
+
const proxyTable = {};
|
|
87
|
+
if (proxyArg.endsWith('.js') || proxyArg.endsWith('.json')) {
|
|
88
|
+
const proxyOptFile = resolve(process.cwd(), proxyArg);
|
|
89
|
+
if (!existsSync(proxyOptFile)) {
|
|
90
|
+
console.error(`proxy-options: 文件 ${proxyOptFile} 不存在`);
|
|
91
|
+
process.exit();
|
|
92
|
+
}
|
|
93
|
+
const proxyObj = require(proxyOptFile);
|
|
94
|
+
Object.keys(proxyObj).forEach(function (h) {
|
|
95
|
+
proxyTable[h] = proxyObj[h];
|
|
96
|
+
}, this);
|
|
97
|
+
} else {
|
|
98
|
+
try {
|
|
99
|
+
proxyArg.split(/\s*,\s*/).forEach(function (h) {
|
|
100
|
+
const [origin, target] = h.split('|');
|
|
101
|
+
proxyTable[origin] = target;
|
|
102
|
+
}, this);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.warn(colors.yellow('\nParse web proxy options Failed:', error));
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
process.env.PROXY_OPTIONS = JSON.stringify(proxyTable);
|
|
72
109
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
110
|
+
if (argv.D || argv['web-dir']) {
|
|
111
|
+
process.env.WEB_ROOT = resolve(process.cwd(), argv.D || argv['web-dir']);
|
|
112
|
+
if (!existsSync(process.env.WEB_ROOT)) {
|
|
113
|
+
console.error(`web-dir: 目录 ${process.env.WEB_ROOT} 不存在`);
|
|
114
|
+
process.exit();
|
|
115
|
+
}
|
|
116
|
+
// Web Server port
|
|
117
|
+
portfinder.basePort = argv.P || argv['web-port'] || 9090;
|
|
76
118
|
portfinder.getPort(function (err, port) {
|
|
77
119
|
if (err) {
|
|
78
120
|
throw err;
|
|
79
121
|
}
|
|
80
|
-
process.env.
|
|
122
|
+
process.env.WEB_PORT = port;
|
|
81
123
|
});
|
|
82
|
-
} else {
|
|
83
|
-
process.env.PORT = port;
|
|
84
124
|
}
|
|
85
125
|
|
|
126
|
+
// Mock Server port
|
|
127
|
+
portfinder.basePort = port || 8090;
|
|
128
|
+
portfinder.getPort(function (err, port) {
|
|
129
|
+
if (err) {
|
|
130
|
+
throw err;
|
|
131
|
+
}
|
|
132
|
+
process.env.PORT = port;
|
|
133
|
+
});
|
|
134
|
+
|
|
86
135
|
const watchMockFiles = function (watchDir) {
|
|
87
136
|
/**
|
|
88
137
|
* script 重启的脚本
|
|
89
138
|
* ext 检测的文件
|
|
90
139
|
*/
|
|
91
140
|
nodemon({
|
|
92
|
-
script:
|
|
141
|
+
script: resolve(__dirname, '../lib/mockServer.js'),
|
|
93
142
|
watch: [watchDir],
|
|
94
143
|
ext: 'js,json'
|
|
95
144
|
});
|
|
@@ -111,20 +160,20 @@ const watchMockFiles = function (watchDir) {
|
|
|
111
160
|
};
|
|
112
161
|
// Node子进程启动MockServer
|
|
113
162
|
const startMockServer = function () {
|
|
114
|
-
spawn(node, [
|
|
163
|
+
spawn(node, [resolve(__dirname, '../lib/mockServer.js')], {
|
|
115
164
|
stdio: 'inherit'
|
|
116
165
|
});
|
|
117
166
|
};
|
|
118
167
|
|
|
119
168
|
if (specifiedFile) {
|
|
120
|
-
process.env.SPECIFIED_FILE =
|
|
169
|
+
process.env.SPECIFIED_FILE = resolve(process.cwd(), specifiedFile);
|
|
121
170
|
if (isStartSocketServer) {
|
|
122
171
|
startMockServer();
|
|
123
172
|
} else {
|
|
124
173
|
watchMockFiles(process.env.SPECIFIED_FILE);
|
|
125
174
|
}
|
|
126
175
|
} else if (watchDir) {
|
|
127
|
-
process.env.SPECIFIED_DIR =
|
|
176
|
+
process.env.SPECIFIED_DIR = resolve(process.cwd(), watchDir);
|
|
128
177
|
// log.info('SPECIFIED_DIR::', process.env.SPECIFIED_DIR);
|
|
129
178
|
if (isStartSocketServer) {
|
|
130
179
|
startMockServer();
|
|
@@ -132,7 +181,7 @@ if (specifiedFile) {
|
|
|
132
181
|
watchMockFiles(process.env.SPECIFIED_DIR);
|
|
133
182
|
}
|
|
134
183
|
} else {
|
|
135
|
-
watchDir = process.env.SPECIFIED_DIR =
|
|
184
|
+
watchDir = process.env.SPECIFIED_DIR = resolve(process.cwd(), './mock');
|
|
136
185
|
// log.info('SPECIFIED_DIR::', process.env.SPECIFIED_DIR);
|
|
137
186
|
if (isStartSocketServer) {
|
|
138
187
|
startMockServer();
|
package/lib/mockServer.js
CHANGED
|
@@ -5,9 +5,18 @@ const express = require('express'), // 引入express
|
|
|
5
5
|
colors = require('colors/safe'),
|
|
6
6
|
portfinder = require('portfinder'),
|
|
7
7
|
chalk = require('chalk');
|
|
8
|
+
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
8
9
|
const ifaces = os.networkInterfaces();
|
|
9
10
|
const { Server } = require('socket.io');
|
|
10
|
-
const {
|
|
11
|
+
const {
|
|
12
|
+
dateFormat,
|
|
13
|
+
logger,
|
|
14
|
+
SupportMethods,
|
|
15
|
+
DefaultHeaders,
|
|
16
|
+
getFileLatestContent,
|
|
17
|
+
filePath2ApiUrl,
|
|
18
|
+
isEmptyObj
|
|
19
|
+
} = require('./utils');
|
|
11
20
|
const { genMockFiles, getMockStatFromDir, getMockStatFromFile } = require('./manageMockFiles');
|
|
12
21
|
|
|
13
22
|
const methodRegExp = new RegExp(`(${SupportMethods.join('|')}) +(.*)`, 'i');
|
|
@@ -46,12 +55,12 @@ if (process.env.CORS_ORIGIN) {
|
|
|
46
55
|
process.env.CORS_ORIGIN.split(/\s*,\s*/).forEach(function (h) {
|
|
47
56
|
corsOrigin.push(h);
|
|
48
57
|
}, this);
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
|
|
59
|
+
corsHeaders = DefaultHeaders;
|
|
51
60
|
}
|
|
52
61
|
if (process.env.CORS_HEADERS) {
|
|
53
62
|
process.env.CORS_HEADERS.split(/\s*,\s*/).forEach(function (h) {
|
|
54
|
-
corsHeaders += ', ' + h;
|
|
63
|
+
corsHeaders += corsHeaders ? ', ' + h : h;
|
|
55
64
|
}, this);
|
|
56
65
|
}
|
|
57
66
|
/**
|
|
@@ -143,13 +152,62 @@ const parseMockFiles = function (specialDir = '../mock') {
|
|
|
143
152
|
composeRouteFromJsFile(filePath);
|
|
144
153
|
}
|
|
145
154
|
};
|
|
155
|
+
|
|
156
|
+
// 允许跨域
|
|
146
157
|
app.use(crossDomain());
|
|
158
|
+
|
|
147
159
|
if (process.env.SPECIFIED_FILE) {
|
|
148
160
|
composeRouteFromJsFile(process.env.SPECIFIED_FILE);
|
|
149
161
|
} else {
|
|
150
162
|
parseMockFiles(process.env.SPECIFIED_DIR);
|
|
151
163
|
}
|
|
152
164
|
argv.a && log.info(chalk.hex('#ce5b34bd')(`${fileCount} mock file are parsed in total.`));
|
|
165
|
+
|
|
166
|
+
let webApp = null,
|
|
167
|
+
webPublicPath = '/',
|
|
168
|
+
proxyTable = {};
|
|
169
|
+
// Enable Web Server
|
|
170
|
+
if (process.env.WEB_ROOT) {
|
|
171
|
+
webApp = express();
|
|
172
|
+
// Enable web proxy
|
|
173
|
+
if (process.env.PROXY_OPTIONS) {
|
|
174
|
+
try {
|
|
175
|
+
const options = JSON.parse(process.env.PROXY_OPTIONS);
|
|
176
|
+
Object.keys(options).forEach(function (origin) {
|
|
177
|
+
proxyTable[origin] = options[origin];
|
|
178
|
+
// 接口代理
|
|
179
|
+
webApp.use(origin, createProxyMiddleware({ target: options[origin], changeOrigin: true, ws: true }));
|
|
180
|
+
}, this);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.warn(colors.yellow('\nEnable web proxy Failed:', error));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// console.log(process.env.WEB_PUBLIC_PATH, process.env.WEB_ROOT);
|
|
186
|
+
if (process.env.WEB_PUBLIC_PATH) {
|
|
187
|
+
webPublicPath = process.env.WEB_PUBLIC_PATH;
|
|
188
|
+
}
|
|
189
|
+
webApp.use(webPublicPath, express.static(process.env.WEB_ROOT)); // 将dist目录下所有文件作为静态文件来管理
|
|
190
|
+
const defaultStaticEntry = `${process.env.WEB_ROOT}/index.html`;
|
|
191
|
+
if (fs.existsSync(defaultStaticEntry)) {
|
|
192
|
+
webApp.get('*', (req, res) => {
|
|
193
|
+
res.sendFile(defaultStaticEntry);
|
|
194
|
+
});
|
|
195
|
+
} else {
|
|
196
|
+
webApp.use(webPublicPath, (req, res) => {
|
|
197
|
+
res.status(200)
|
|
198
|
+
.send(`Hi, welcome to web server entrance ui, you can test http proxy on browser console or api request tools. Examples as follows:<br />
|
|
199
|
+
fetch("http://localhost:${process.env.WEB_PORT}/api/permission/auth/login", {
|
|
200
|
+
method: 'post',
|
|
201
|
+
headers: {
|
|
202
|
+
"accept": "*/*",
|
|
203
|
+
"Content-type": "application/json; charset=UTF-8",
|
|
204
|
+
},
|
|
205
|
+
body:JSON.stringify({"loginName":"admin","password":"admin"})
|
|
206
|
+
})
|
|
207
|
+
`);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
153
211
|
const http = require('http').createServer(app);
|
|
154
212
|
if (process.env.SOCKET_SERVER) {
|
|
155
213
|
socketServer = new Server(http, { path: '/ws/mock-service' });
|
|
@@ -209,7 +267,8 @@ http.listen(Number.parseInt(process.env.PORT, 10), () => {
|
|
|
209
267
|
console.info(
|
|
210
268
|
[colors.yellow('\n🌍 mock-server version: '), colors.cyan(require('../package.json').version), '\n'].join('')
|
|
211
269
|
);
|
|
212
|
-
|
|
270
|
+
console.info(colors.yellow(`\n Mock server available on:\n`));
|
|
271
|
+
console.info(' http://localhost:' + colors.green(process.env.PORT));
|
|
213
272
|
Object.keys(ifaces).forEach(function (dev) {
|
|
214
273
|
ifaces[dev].forEach(function (details) {
|
|
215
274
|
if (details.family === 'IPv4') {
|
|
@@ -220,6 +279,26 @@ http.listen(Number.parseInt(process.env.PORT, 10), () => {
|
|
|
220
279
|
}
|
|
221
280
|
});
|
|
222
281
|
|
|
282
|
+
if (webApp && !process.env.RESTARTED) {
|
|
283
|
+
webApp.listen(Number.parseInt(process.env.WEB_PORT, 10), () => {
|
|
284
|
+
console.info(colors.yellow(`\n Web server available on:\n`));
|
|
285
|
+
console.info(' http://localhost:' + colors.green(process.env.WEB_PORT) + webPublicPath);
|
|
286
|
+
Object.keys(ifaces).forEach(function (dev) {
|
|
287
|
+
ifaces[dev].forEach(function (details) {
|
|
288
|
+
if (details.family === 'IPv4') {
|
|
289
|
+
console.info(' http://' + details.address + ':' + colors.green(process.env.WEB_PORT) + webPublicPath);
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
if (!isEmptyObj(proxyTable)) {
|
|
294
|
+
console.info(colors.yellow(`\n Enable proxy at web server on:`));
|
|
295
|
+
Object.keys(proxyTable).forEach(origin => {
|
|
296
|
+
console.info(` ${origin} -> ${proxyTable[origin]}`);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
223
302
|
if (process.platform === 'win32') {
|
|
224
303
|
require('readline')
|
|
225
304
|
.createInterface({
|
package/lib/utils.js
CHANGED
|
@@ -86,7 +86,8 @@ function logger(isSilent = false) {
|
|
|
86
86
|
}
|
|
87
87
|
// MockServer 支持的请求类型
|
|
88
88
|
const SupportMethods = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH', 'OPTIONS', 'COPY', 'LINK', 'UNLINK', 'PURGE'];
|
|
89
|
-
|
|
89
|
+
const DefaultHeaders =
|
|
90
|
+
'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';
|
|
90
91
|
const isValidMethod = function (m) {
|
|
91
92
|
return SupportMethods.includes(String(m).toLocaleUpperCase());
|
|
92
93
|
};
|
|
@@ -279,6 +280,7 @@ module.exports = {
|
|
|
279
280
|
dateFormat,
|
|
280
281
|
logger,
|
|
281
282
|
SupportMethods,
|
|
283
|
+
DefaultHeaders,
|
|
282
284
|
isValidMethod,
|
|
283
285
|
getFileLatestContent,
|
|
284
286
|
filePath2ApiUrl,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mock-service-cli",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "🦅 Local mock server",
|
|
5
5
|
"main": "./lib/mockServer.js",
|
|
6
6
|
"bin": {
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"deasync": "^0.1.23",
|
|
50
50
|
"express": "^4.17.1",
|
|
51
51
|
"fs-extra": "^10.0.0",
|
|
52
|
+
"http-proxy-middleware": "^2.0.6",
|
|
52
53
|
"lodash": "^4.17.21",
|
|
53
54
|
"minimist": "^1.2.5",
|
|
54
55
|
"nodemon": "^2.0.14",
|