diff --git a/README.md b/README.md index 194f811..b4f25a3 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +只需要更新mock文件夹下的json数据即可操作API + +## 更新记录 + +- 20241015 + - 监听mock目录下文件新增/删除,重启服务 + - 增加proxy代理请求日志 + + +## 待办&问题 + +- proxy代理请求超时 +- 跨域配置 +- 自动生成mock.config.json文件 +- 返回参数处理 +- 路由参数变量匹配 +- 请求参数匹配 +- 判断如果没有mock.config.json,不要报错 +- 操作视频 +- 删除配置文件中swaggerJSON中的type字段 diff --git a/command/middleware.js b/command/middleware.js index ecfe549..c6f4fd5 100644 --- a/command/middleware.js +++ b/command/middleware.js @@ -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(); diff --git a/command/restful.js b/command/restful.js index 6cc54f7..43c8ec9 100644 --- a/command/restful.js +++ b/command/restful.js @@ -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); } diff --git a/package.json b/package.json index 0891f39..92c1c3a 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/server.js b/server.js index 52ba044..856e42e 100644 --- a/server.js +++ b/server.js @@ -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'; @@ -33,7 +34,7 @@ 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)); @@ -41,8 +42,8 @@ class MockServer { app.post('/submit', handleFormSubmission); // 通用中间件 - app.all('*', cors); - app.all('*', timeoutSetting); + app.use(cors()); + app.use('*', timeoutSetting); if (this.type === 'action') { app.all('*', actionTransfer); } diff --git a/yarn.lock b/yarn.lock index 8cb3600..efd4802 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== @@ -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" @@ -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==