const httpProxy = require("http-proxy");
const http = require("http");
const url = require("url");
const proxy = new httpProxy.createProxyServer();
const proxyServer = http.createServer(function (req, res) {
proxy.web(req, res);
});
proxyServer.on("upgrade", function (req, socket, head) {
const { pathname } = url.parse(req.url);
if (pathname.indexOf("/test") === 0) {
proxy.ws(req, socket, {
target: `http://127.0.0.1:10002/`,
changeOrigin: true, // 等用于 req.headers.host = "127.0.0.1:10002"
ignorePath: true, // 等同于 req.url="/"
});
}
});
proxyServer.listen(8000);
网友评论