1
+ import type { IncomingMessage , ServerResponse } from 'node:http' ;
1
2
import http from 'node:http' ;
2
3
import type { createServer as _createServer , Server , ServerOptions } from 'node:https' ;
3
4
import https from 'node:https' ;
@@ -14,14 +15,23 @@ type ProxyServerOptions = {
14
15
* The server will listen on port 80 (http) or 443 (https) depending on whether SSL options are provided.
15
16
*/
16
17
export const createProxyServer = ( opts : ProxyServerOptions ) => {
17
- const proxy = httpProxy . createProxyServer ( { xfwd : true } ) ;
18
18
const usingSSL = ! ! opts . ssl ;
19
+
20
+ const proxy = httpProxy . createProxyServer ( {
21
+ secure : usingSSL ,
22
+ xfwd : true ,
23
+ } ) ;
24
+
25
+ // We need to handle errors to avoid crashing the proxy server
26
+ proxy . on ( 'error' , ( err : Error , req : IncomingMessage , res : ServerResponse ) => {
27
+ console . error ( `[Proxy Error]: ${ req . url } ` , err ) ;
28
+ res . writeHead ( 502 ) ;
29
+ res . end ( 'Proxy error' ) ;
30
+ } ) ;
31
+
19
32
const createServer : typeof _createServer = usingSSL ? https . createServer . bind ( https ) : http . createServer . bind ( http ) ;
20
33
21
34
return createServer ( opts . ssl , ( req , res ) => {
22
- console . log ( `/n/n/n/n------------------------------------` ) ;
23
- console . log ( 'Proxying request' , req . headers . host , req . url ) ;
24
- console . log ( 'Headers' , req . headers ) ;
25
35
const hostHeader = req . headers . host || '' ;
26
36
if ( opts . targets [ hostHeader ] ) {
27
37
proxy . web ( req , res , { target : opts . targets [ hostHeader ] } ) ;
0 commit comments