Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns
Node.js HTTP connect event
How the 'connect' event works on a Node.js http.Server and ClientRequest: the CONNECT method, building a TCP tunnel for an HTTP proxy, and its pitfalls.
The HTTP CONNECT method asks a server to open a raw TCP connection to some other host and then simply relay bytes in both directions. Its main use is HTTPS through a proxy: the browser sends CONNECT example.com:443, the proxy connects to that address, answers 200, and from then on forwards the encrypted bytes without looking inside them.
Node exposes this as a separate event. An http.Server emits 'connect' (not 'request') when it receives a CONNECT request, giving you the raw client socket. An http.ClientRequest made with method: 'CONNECT' emits 'connect' when the proxy answers with a success status. Building a tunnel is a matter of wiring two sockets together.
proxy.on('connect', (req, clientSocket, head) => { const [host, port] = req.url.split(':'); const upstream = net.connect(Number(port), host, () => { clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n'); upstream.pipe(clientSocket); clientSocket.pipe(upstream); }); });
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
How it works
Server side. The listener receives (request, socket, head). request.url is not a path but an authority, host:port. socket is the client's connection, and head is a Buffer with bytes that arrived after the request headers (for a TLS handshake, an eager client may already have sent its first message). You reply with a status line written by hand. A 2xx status tells the client the tunnel is open; after that the connection carries whatever the client and the destination say to each other.
Client side. req.on('connect', (response, socket, head) => ...) fires whenever the server answers a CONNECT request, whatever the status, so check response.statusCode before treating the socket as a tunnel (the second example does). If you attach no 'connect' listener, the connection is closed when the answer arrives.
HTTPS through a proxy. After the tunnel is open, you give the tunnelled socket to a TLS client (tls.connect({ socket, servername }) or the createConnection option of https.request()) and TLS runs end to end through the proxy. The proxy sees only the destination name and the byte counts.
Usage notes
- Restrict what you tunnel. An open
CONNECTproxy lets anyone open connections from your machine to anywhere, including internal services. Allow-list hosts and ports, require authentication (Proxy-Authorization), and listen only on trusted interfaces. - Wire errors both ways. Destroy the other socket when one side fails, otherwise half-open connections pile up.
- Write
headupstream when it is not empty; dropping it loses the client's first bytes. 'connect'is not for ordinary requests. Normal methods go to'request';Upgraderequests go to'upgrade'(see The upgrade event).- Environment proxies: Node can read
HTTP_PROXY/HTTPS_PROXYforfetch()andhttpwhenNODE_USE_ENV_PROXY=1is set; the proxy then receivesCONNECTfor HTTPS URLs. - HTTP/2 has its own
CONNECTform, handled on streams rather than through this event.