Skip to content

Commit

Permalink
fix: proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
code-icu committed Oct 15, 2024
1 parent 54c73c2 commit c5c179c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,23 @@ swagger文档生成后,只需要拿到当前swagger的json文件,写入配
docker run -d -p 9000:9000 -v /data/api/mock:/app/mock -v data/api/mock.config.json:/app/mock.config.json lvpf/dev-mock-cli
```

只需要更新mock文件夹下的json数据即可操作API
只需要更新mock文件夹下的json数据即可操作API

## 更新记录

- 20241015
- 监听mock目录下文件新增/删除,重启服务
- 增加proxy代理请求日志


## 待办&问题

- proxy代理请求超时
- 跨域配置
- 自动生成mock.config.json文件
- 返回参数处理
- 路由参数变量匹配
- 请求参数匹配
- 判断如果没有mock.config.json,不要报错
- 操作视频
- 删除配置文件中swaggerJSON中的type字段
2 changes: 1 addition & 1 deletion command/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setHeader } from '../utils/index.js';
export const cors = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*'); //访问控制允许来源:所有
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); //访问控制允许报头 X-Requested-With: xhr请求
res.header('Access-Control-Allow-Metheds', 'PUT, POST, GET, DELETE, OPTIONS'); //访问控制允许方法
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); //访问控制允许方法
res.header('X-Powered-By', 'nodejs'); //自定义头信息,表示服务端用nodejs
res.header('Content-Type', 'application/json;charset=utf-8');
next();
Expand Down
4 changes: 4 additions & 0 deletions command/restful.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const restful = async ({ app, filePath, config }) => {
const proxyMiddleware = createProxyMiddleware({
target: proxyApiUrl,
changeOrigin: true,
logLevel: 'debug',
onProxyRes(proxyRes, req, res) {
console.log(`[Proxy Response] Status: ${proxyRes.statusCode}, Path: ${req.path}`);
}
})
app.use('*', proxyMiddleware);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npx nodemon ./bin/index.js mock",
"start": "npx nodemon --watch ./mock --exec 'node ./bin/index.js mock'",
"prettier": "prettier -w .",
"publish:patch": "npm version patch && npm publish --registry=https://registry.npmjs.org"
},
Expand All @@ -28,6 +28,7 @@
"dependencies": {
"axios": "^1.7.7",
"chalk": "4.1.2",
"cors": "^2.8.5",
"express": "^4.19.2",
"get-port": "^7.1.0",
"http-proxy-middleware": "^2.0.6",
Expand Down
9 changes: 5 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import express from 'express';
import path from 'path';
import cors from 'cors';

import { cors, timeoutSetting, actionTransfer } from './command/middleware.js';
import { timeoutSetting, actionTransfer } from './command/middleware.js';
import restful from './command/restful.js';
import action from './command/action.js';
import { ConfirmPort } from './utils/prompt.js';
Expand Down Expand Up @@ -33,16 +34,16 @@ class MockServer {
}

const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
app.use(express.json({ limit: '50mb' }));
app.use(express.static(this.staticPath));

app.get('/monto/docs', (req, res) => res.sendFile(path.join(this.staticPath, 'index.html')));
app.post('/submit', handleFormSubmission);

// 通用中间件
app.all('*', cors);
app.all('*', timeoutSetting);
app.use(cors());
app.use('*', timeoutSetting);
if (this.type === 'action') {
app.all('*', actionTransfer);
}
Expand Down
20 changes: 13 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1750,14 +1750,15 @@ core-js-compat@^3.36.1, core-js-compat@^3.37.1:
dependencies:
browserslist "^4.23.0"

cross-env@^7.0.3:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz#865264b29677dc015ba8418918965dd232fc54cf"
integrity sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==
cors@^2.8.5:
version "2.8.5"
resolved "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
dependencies:
cross-spawn "^7.0.1"
object-assign "^4"
vary "^1"

cross-spawn@^7.0.1, cross-spawn@^7.0.2:
cross-spawn@^7.0.2:
version "7.0.3"
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
Expand Down Expand Up @@ -2693,6 +2694,11 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

object-assign@^4:
version "4.1.1"
resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==

object-inspect@^1.9.0:
version "1.12.2"
resolved "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.2.tgz#c0641f26394532f28ab8d796ab954e43c009a8ea"
Expand Down Expand Up @@ -3308,7 +3314,7 @@ validator@^13.7.0:
resolved "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz#7d78e76ba85504da3fee4fd1922b385914d4b35f"
integrity sha512-c1Q0mCiPlgdTVVVIJIrBuxNicYE+t/7oKeI9MWLj3fh/uq2Pxh/3eeWbVZ4OcGW1TUf53At0njHw5SMdA3tmMg==

vary@~1.1.2:
vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==
Expand Down

0 comments on commit c5c179c

Please sign in to comment.