關於這個問題的一個奇怪的事情是,在我離開我的桌子之前,我有一個工作的例子(儘管在握手後沒有數據被傳輸),現在儘管恢復了我所做的一些小改動,但似乎無法正常工作。NodeJS + http代理 - 對象沒有方法'resume'
我使用NodeJS + http代理模塊將.html文件的請求代理到Apache,並且所有其他請求都由NodeJS處理。
這裏是我的server.js代碼:
CometServer = (function() {
var instance;
function __construct() {
var apachePort = 3000;
var nodeJsPort = 3001;
var apacheHost = 'localhost';
var nodeJsHost = 'localhost';
var httpProxy = require('/root/node_modules/http-proxy');
var io = require('/root/node_modules/socket.io');
var fs = require('/root/node/lib/fs');
var path = require('/root/node/lib/path');
var url = require('/root/node/lib/url');
var apacheUrls = [
/.*\.html/,
/^\/$/
];
function proxyRequests(request, response, proxy) {
// Check if the URL is a comet request or not.
var shouldProxy = apacheUrls.some(function(requestUrl) {
return requestUrl.test(request.url);
});
// If 'request.url' matches any of the 'apacheUrls' then proxy to Apache.
if (shouldProxy) {
return proxy.proxyRequest(request, response, {
host: apacheHost,
port: apachePort
});
}
// Not proxied to Apache; this is a comet request and should be processed by NodeJS.
return handleUnproxiedRequests(request, response);
}
function handleUnproxiedRequests(request, response) {
var uri = url.parse(request.url).pathname;
var filePath = path.join(process.cwd(), '..', uri);
var extname = path.extname(filePath);
var contentType = 'text/javascript';
path.exists(filePath, function(exists) {
if (exists) {
var fileStream = fs.createReadStream(filePath);
fileStream.on('data', function (data) {
response.writeHead(200, {
'Content-Type': contentType
});
response.write(data);
response.end();
});
}
else {
response.writeHead(404);
response.end();
}
});
return;
}
function bootstrap() {
// Create a http proxy server and use the 'proxy' conditionally inside the request handler.
var server = httpProxy.createServer(nodeJsPort, nodeJsHost, {
target: {
host: nodeJsHost,
port: nodeJsPort
}
}, proxyRequests);
// Get the http proxy server to listen to a port.
server.listen(nodeJsPort);
// Get socket.io to listen to the proxy server.
var listener = io.listen(server);
var something = listener.on('connection', function(socket) {
console.log('socket connected');
socket.on('handshake', function(pid) {
console.log('socket? ' + pid);
})
});
}
// Bootstrap server.
bootstrap();
return { }
}
return {
getInstance: function() {
// Instantiate only if the instance doesn't already exist.
if (!instance) {
instance = __construct();
}
return instance;
}
}
})();
// Initialize the comet server.
CometServer.getInstance();
和客戶端JS:
var socket = io.connect();
console.log('Connected');
socket.on('connect', function(data) {
console.log('Sending handshake');
socket.emit('handshake', 1);
});
請注意,此代碼是目前有點混亂,因爲它是在非常早期的階段,生產。然而,如果你認爲我可以/應該以不同的方式做某件事,你可以隨意批評它。
這是我收到錯誤消息當我嘗試訪問的.html頁面:
/root/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:334
? buffer.resume()
^
TypeError: Object #<Object> has no method 'resume'
at [object Function].proxyRequest (/root/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:334:16)
at proxyRequests (/var/www/html/Comet/server.js:139:22)
at Server.<anonymous> (/root/node_modules/http-proxy/lib/node-http-proxy.js:321:7)
at Manager.handleRequest (/root/node_modules/socket.io/lib/manager.js:531:28)
at Server.<anonymous> (/root/node_modules/socket.io/lib/manager.js:112:10)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1479:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1375:22)
at TCP.onread (net.js:334:27)
從我讀過的東西,它看起來像數據被緩衝,而不是流,但我不真的有任何想法如何解決這個問題,因爲我對API非常陌生(這是我的第一個NodeJS項目)。
任何幫助,將不勝感激,因爲我已經撞了一段時間,我的頭撞磚牆了很長一段時間了。
我也試着故意傳遞一個緩衝的proxyRequest:如下圖所示: VAR緩衝= httpProxy.buffer(要求); return proxy.proxyRequest(request,response,{ port:apachePort, host:apacheHost, buffer:buffer }); 不幸的是,我仍然得到了相同的結果。 – GMemory 2012-01-10 15:22:15