http.Server的“连接”事件永远不会被调用
我创build一个简单的http服务器,我想通过套接字传输一些字节的数据。 所以我正在听服务器的“连接”事件。 但它从来没有被称为?
这是我的代码。
var http = require('http'); var server = http.createServer(function(req, res) { res.setHeader('Content-type', 'text/html'); res.end('<h3>Yeah! you are connected on ' + Date() + '</h3>'); console.log('User connected'); }); server.on('connect', function(req, socket, head) { //var addr = socket.remoteAddress; console.log('IP - '); }); server.listen(8000);
对于connect
事件,当服务器运行时,需要向隧道代理请求。
replace你的server.listen(8000);
有了这个:
server.listen(8000, '127.0.0.1', () => { // make a request to a tunneling proxy const options = { port: 8000, hostname: '127.0.0.1', method: 'CONNECT', path: 'www.google.com:80' }; const req = http.request(options); req.end(); });