From c4e31052652e304f80093c5c17c25318a9f02625 Mon Sep 17 00:00:00 2001 From: Shirtiny Date: Tue, 29 Jun 2021 15:56:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5proxy=E5=92=8Cproxy=E7=94=9F?= =?UTF-8?q?=E6=95=88=E6=97=B6`=E7=9A=84=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .node/builder.js | 4 ++-- .node/logger.js | 11 ++++++----- .node/server.js | 31 ++++++++++++++++++++++++++----- .sh.js | 8 +++++++- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/.node/builder.js b/.node/builder.js index 94e9028..8d1a78e 100644 --- a/.node/builder.js +++ b/.node/builder.js @@ -1,7 +1,7 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 17:41:22 - * @LastEditTime: 2021-06-27 09:52:15 + * @LastEditTime: 2021-06-29 15:48:33 * @Description: */ const esbuild = require("esbuild"); @@ -50,7 +50,7 @@ const build = async ({ entryPoints = [], platform, outfile, plugins = [] }) => { outfile, plugins, }); - logger.chan("Building", entryPoints, outfile); + logger.chan("Building", [entryPoints], outfile); } catch (e) { return console.error(e.message); } diff --git a/.node/logger.js b/.node/logger.js index ac4e956..3c97aad 100644 --- a/.node/logger.js +++ b/.node/logger.js @@ -1,7 +1,7 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 21:18:47 - * @LastEditTime: 2021-06-26 21:56:29 + * @LastEditTime: 2021-06-29 15:55:16 * @Description: */ @@ -11,11 +11,12 @@ const log = (...messages) => { console.log(chalk.hex("#00b7c3")(...messages)); }; -const chan = (title = "", input = "", output = "") => { +const chan = (title = "", inputs = [], output = "") => { + const input = inputs.filter(i => i).map(i => chalk.blueBright.underline( + `${i}`, + )).join(` ${chalk.cyan("->")} `) console.log( - `${chalk.greenBright.bold(title)} ${chalk.blueBright.underline.italic( - `${input}`, - )} ${chalk.cyan("->")} ${chalk.yellowBright(output)}`, + `${chalk.greenBright.bold(title)} ${input} ${chalk.cyan("->")} ${chalk.yellowBright(output)}`, ); }; diff --git a/.node/server.js b/.node/server.js index 507afd2..eb7d7be 100644 --- a/.node/server.js +++ b/.node/server.js @@ -1,7 +1,7 @@ /* * @Author: Shirtiny * @Date: 2021-06-25 17:35:25 - * @LastEditTime: 2021-06-26 22:25:18 + * @LastEditTime: 2021-06-29 15:55:05 * @Description: */ "use strict"; @@ -22,6 +22,25 @@ const distDirPath = path.resolve(__dirname, "../dist"); const srcFileName = "index.ts"; const distFileName = "index.js"; + +// 需代理的路径列表 +const proxyPathList = Object.keys(config.devServer?.proxy || {}); + +// 返回一个替换后的URL对象 +const createDevProxyURL = (reqUrl = "") => { + const proxy = config.devServer?.proxy; + if (!proxy) return null; + + const proxyPath = proxyPathList.filter((p) => new RegExp(p).test(reqUrl))[0]; + if (proxyPath) { + const { target, pathRewrite } = proxy[proxyPath]; + const rewriteKey = Object.keys(pathRewrite || {})[0]; + if (!rewriteKey) return new URL(`${target}${reqUrl}`); + return new URL(`${target}${reqUrl.replace(new RegExp(rewriteKey), pathRewrite[rewriteKey])}`) + } + return null +} + const serve = async () => { await util.mkdir(distDirPath); util.cpAllDirChildsToDir(publicDirPath, distDirPath); @@ -48,17 +67,19 @@ const serve = async () => { http .createServer((req, res) => { + const proxyURL = createDevProxyURL(req.url) + const options = { - hostname: host, - port: port, - path: req.url, + hostname: proxyURL?.hostname || host, + port: proxyURL?.port || port, + path: proxyURL?.pathname || req.url, method: req.method, headers: req.headers, }; const proxyReq = http.request(options, (proxyRes) => { res.writeHead(proxyRes.statusCode, proxyRes.headers); - logger.chan(req.method.toUpperCase(), req.url, res.statusCode); + logger.chan(req.method.toUpperCase(), [req.url, proxyURL ? `${proxyURL.hostname}:${proxyURL.port}${proxyURL.pathname}` : ""], res.statusCode); proxyRes.pipe(res, { end: true }); }); diff --git a/.sh.js b/.sh.js index 8fd7ee1..4d3d034 100644 --- a/.sh.js +++ b/.sh.js @@ -1,7 +1,7 @@ /* * @Author: Shirtiny * @Date: 2021-06-26 20:47:19 - * @LastEditTime: 2021-06-26 21:01:58 + * @LastEditTime: 2021-06-29 15:44:23 * @Description: */ module.exports = { @@ -9,5 +9,11 @@ module.exports = { devServer: { host: "localhost", port: 2021, + proxy: { + '^/api': { + target: 'http://192.168.6.111:9780', + pathRewrite: { '^/api': '' }, + }, + } }, };