var httpProxy = require(‘http-proxy’); var utilsCommon = require(“./xxx/libs/utilsCommon”); var common_time = utilsCommon.getUTC8Time(“YYYY-MM-DD HH:mm:ss”);
httpProxy.createServer(function (req, res, proxy) { var host = req.headers.host; var port = 1000; var newhost = host.replace(/www./i, “”); switch (newhost) { case "a.com": port = 1000; break; case "b.com": port = 2000; break; case "c.com": port = 3000; break; } proxy.proxyRequest(req, res, { host: 'localhost’, port: port, timeout:60000 }); }).listen(80);
console.log("proxy if listening 80 port ");
经常性使用几个小时 该进程 内存占70 % - 80 % 的样子
控制台信息要么是
/opt/www/proxy.js:8 var newhost = host.replace(/www./i, “”); ^ TypeError: Cannot call method ‘replace’ of undefined at Array.fs.writeFile.encoding [as 0] (/opt/www/proxy.js:8:24) at Server.handler (/opt/www/node_modules/http-proxy/lib/node-http-proxy.js:175:40) at Server.EventEmitter.emit (events.js:98:17) at HTTPParser.parser.onIncoming (http.js:2012:12) at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:119:23) at Socket.socket.ondata (http.js:1902:22) at TCP.onread (net.js:510:27)(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at Socket.EventEmitter.addListener (events.js:160:15) at Socket.Readable.on (_stream_readable.js:653:33) at Socket.EventEmitter.once (events.js:179:8) at TCP.onread (net.js:527:26)
要么是 proxy if listening 80 port (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at Socket.EventEmitter.addListener (events.js:160:15) at Socket.Readable.on (_stream_readable.js:653:33) at Socket.EventEmitter.once (events.js:179:8) at TCP.onread (net.js:527:26) [root[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@localhost](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost)](/user/localhost) www]# node proxy.js
大家有遇到么 还是代码写的有问题
/opt/www/proxy.js:8
var newhost = host.replace(/www./i, "");
^
TypeError: Cannot call method 'replace' of undefined
意思是 host 的值为undefined, undefined是没有replace方法的。
你的代码中,host变量是从这里得到的:
var host = req.headers.host;
但是,并不是所有的请求头都必须有host这一项的,所以你应该多加一个判断没有host的情况,或者把代码改成这样:
var host = req.headers.host || '';
var httpProxy = require(‘http-proxy’); var options = { hostnameOnly: true, router: { 'www.a.com’: '127.0.0.1:3000’, 'a.com’: '127.0.0.1:3000’, 'www.b.com’: '127.0.0.1:1000’, 'b.com’: ‘127.0.0.1:1000’ } }; var proxyServer = httpProxy.createServer(options); proxyServer.listen(80); console.log(“proxy is listening 80”);
改用文档上的 这种形式 过一小时左右同样 真怀疑这模块跟最新版nodejs 不兼容
proxy is listening 80 (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at Socket.EventEmitter.addListener (events.js:160:15) at Socket.Readable.on (_stream_readable.js:653:33) at Socket.EventEmitter.once (events.js:179:8) at TCP.onread (net.js:527:26) (node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace at Socket.EventEmitter.addListener (events.js:160:15) at Socket.Readable.on (_stream_readable.js:653:33) at Socket.EventEmitter.once (events.js:179:8) at TCP.onread (net.js:527:26) ~
http-proxy 0.10.2 nodejs v0.10.3