Skip to content

Commit

Permalink
加入proxy和proxy生效时`的打印
Browse files Browse the repository at this point in the history
  • Loading branch information
Shirtiny committed Jun 29, 2021
1 parent 9460e14 commit c4e3105
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .node/builder.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 6 additions & 5 deletions .node/logger.js
Original file line number Diff line number Diff line change
@@ -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:
*/

Expand All @@ -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)}`,
);
};

Expand Down
31 changes: 26 additions & 5 deletions .node/server.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand All @@ -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 });
});

Expand Down
8 changes: 7 additions & 1 deletion .sh.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/*
* @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 = {
globalName: "tsLibTemplate",
devServer: {
host: "localhost",
port: 2021,
proxy: {
'^/api': {
target: 'http://192.168.6.111:9780',
pathRewrite: { '^/api': '' },
},
}
},
};

0 comments on commit c4e3105

Please sign in to comment.