File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,8 +23,13 @@ const http = require('http');
2323const port = parseInt (process .argv [2 ] || ' 8080' , 10 );
2424
2525const server = http .createServer ((req , res ) => {
26- const url = new URL (req .url , ` http://${ req .headers .host } ` );
27- if (url .pathname === ' /echo' ) {
26+ let pathname;
27+ try {
28+ pathname = new URL (req .url , ` http://${ req .headers .host || ' localhost' } ` ).pathname ;
29+ } catch {
30+ pathname = req .url ;
31+ }
32+ if (pathname === ' /echo' ) {
2833 let body = ' ' ;
2934 for (const [name , value ] of Object .entries (req .headers )) {
3035 if (Array .isArray (value)) value .forEach (v => body += name + ' : ' + v + ' \n ' );
Original file line number Diff line number Diff line change @@ -3,8 +3,13 @@ const http = require('http');
33const port = parseInt ( process . argv [ 2 ] || '8080' , 10 ) ;
44
55const server = http . createServer ( ( req , res ) => {
6- const url = new URL ( req . url , `http://${ req . headers . host } ` ) ;
7- if ( url . pathname === '/echo' ) {
6+ let pathname ;
7+ try {
8+ pathname = new URL ( req . url , `http://${ req . headers . host || 'localhost' } ` ) . pathname ;
9+ } catch {
10+ pathname = req . url ;
11+ }
12+ if ( pathname === '/echo' ) {
813 let body = '' ;
914 for ( const [ name , value ] of Object . entries ( req . headers ) ) {
1015 if ( Array . isArray ( value ) ) value . forEach ( v => body += name + ': ' + v + '\n' ) ;
You can’t perform that action at this time.
0 commit comments