response{http.IncomingMessage}socket{stream.Duplex}head{Buffer}
每次服务器使用 CONNECT 方法响应请求时都会触发。
如果未监听此事件,则接收 CONNECT 方法的客户端将关闭其连接。
此事件保证传入 {net.Socket} 类({stream.Duplex} 的子类)的实例,除非用户指定了 {net.Socket} 以外的套接字类型。
客户端和服务器对演示了如何监听 'connect' 事件:
const http = require('http');const net = require('net');const { URL } = require('url');// 创建 HTTP 隧道代理。const proxy = http.createServer((req, res) => {res.writeHead(200, { 'Content-Type': 'text/plain' });res.end('响应内容');});proxy.on('connect', (req, clientSocket, head) => {// 连接到原始服务器。const { port, hostname } = new URL(`http://${req.url}`);const serverSocket = net.connect(port || 80, hostname, () => {clientSocket.write('HTTP/1.1 200 Connection Established\r\n' +'Proxy-agent: Node.js-Proxy\r\n' +'\r\n');serverSocket.write(head);serverSocket.pipe(clientSocket);clientSocket.pipe(serverSocket);});});// 代理正在运行。proxy.listen(1337, '127.0.0.1', () => {// 向隧道代理发出请求。const options = {port: 1337,host: '127.0.0.1',method: 'CONNECT',path: 'nodejs.cn:80'};const req = http.request(options);req.end();req.on('connect', (res, socket, head) => {console.log('已连接');// 通过 HTTP 隧道发出请求。socket.write('GET / HTTP/1.1\r\n' +'Host: nodejs.cn:80\r\n' +'Connection: close\r\n' +'\r\n');socket.on('data', (chunk) => {console.log(chunk.toString());});socket.on('end', () => {proxy.close();});});});
