File tree 1 file changed +23
-9
lines changed
packages/vite/src/node/server/middlewares
1 file changed +23
-9
lines changed Original file line number Diff line number Diff line change 1
1
import type * as http from 'node:http'
2
+ import type * as net from 'node:net'
2
3
import httpProxy from 'http-proxy'
3
4
import type { Connect } from 'types/connect'
4
5
import type { HttpProxy } from 'types/http-proxy'
@@ -43,16 +44,29 @@ export function proxyMiddleware(
43
44
}
44
45
const proxy = httpProxy . createProxyServer ( opts ) as HttpProxy . Server
45
46
46
- proxy . on ( 'error' , ( err , req , res ) => {
47
- config . logger . error ( `${ colors . red ( `http proxy error:` ) } \n${ err . stack } ` , {
48
- timestamp : true ,
49
- error : err
50
- } )
51
- res
52
- . writeHead ( 500 , {
53
- 'Content-Type' : 'text/plain'
47
+ proxy . on ( 'error' , ( err , req , originalRes ) => {
48
+ // When it is ws proxy, res is net.Socket
49
+ const res = originalRes as http . ServerResponse | net . Socket
50
+ if ( 'req' in res ) {
51
+ config . logger . error (
52
+ `${ colors . red ( `http proxy error:` ) } \n${ err . stack } ` ,
53
+ {
54
+ timestamp : true ,
55
+ error : err
56
+ }
57
+ )
58
+ res
59
+ . writeHead ( 500 , {
60
+ 'Content-Type' : 'text/plain'
61
+ } )
62
+ . end ( )
63
+ } else {
64
+ config . logger . error ( `${ colors . red ( `ws proxy error:` ) } \n${ err . stack } ` , {
65
+ timestamp : true ,
66
+ error : err
54
67
} )
55
- . end ( )
68
+ res . end ( )
69
+ }
56
70
} )
57
71
58
72
if ( opts . configure ) {
You can’t perform that action at this time.
0 commit comments