. 1 const HTTP = the require ( ' HTTP ' ); 2 . 3 the let Server http.createServer = ((REQ, RES) => { . 4 res.end ( ' Hello, World! ' ); . 5 }); . 6 . 7 server.listen ( 3000 , ' 127.0.0.1 ' , () => { . 8 the console.log ( ' server has started! ' ); 9 // off after 5 seconds the server 10 the setTimeout (() => { . 11 // server.close ( ); 12 }, 5000 ); 13 }); 14 15 // Close monitor server 16 server.on ( ' Close ' , () => { . 17 the console.log ( ' server is off! ' ); 18 }); . 19 20 is // monitor server error has occurred 21 is server.on ( ' error ' , (E) => { 22 is IF (e.code === ' EADDRINUSE ' ) { 23 is the console.log ( ' port number to be called! ' ); 24 } The else { 25 the console.log ( ' other errors ' , e.code); 26 is } 27 }); 28 29 // set the timeout 30 server.setTimeout ( 1000 , () => { 31 is the console.log ( ' set the timeout time lS ' ); 32 }); 33 is 34 is server.on ( ' timeout ' , () => { 35 // timeout what to do the operation 36 the console.log ( ' connection has timed out ' ); 37 });
1 let http = require('http'); 2 let fs = require('fs'); 3 let path = require('path'); 4 5 http.createServer((req, res)=>{ 6 console.log(path.join(__dirname, 'request.log')); 7 console.log(req); 8 // 输出request.log文件 9 let out = fs.createWriteStream(path.join(__dirname, 'request.log')); 10 out.write('1) method:' + req.method + '\n'); 11 out.write('2) url:' + req.url + '\n'); 12 out.write('3) headers:' + JSON.stringify(req.headers) + '\n'); 13 out.write('4) httpVersion:' + req.httpVersion + '\n'); 14 }).listen(8080, '127.0.0.1');
1 1) method:GET 2 2) url:/ 3 3) headers:{"host":"localhost:8080","connection":"keep-alive","cache-control":"max-age=0",
"upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/75.0.3770.100 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;
q=0.8,application/signed-exchange;v=b3","accept-encoding":"gzip, deflate, br","accept-language":"zh-CN,zh;q=0.9,en;q=0.8,lb;
q=0.7","cookie":"_nano_fp=XpdYn5CjX0UJnpTJXC_ALVUQMlgQwrkHd5Az6sJb"} 4 4) httpVersion:1.1
1 // embodiment 1 2 . 3 const = the require URL ( 'URL' ); . 4 // string converted into a URL object . 5 const = myURL is url.parse ( 'HTTPS: // User: [email protected]: 8080 / P / A / T / H = Query String the hash # '? ); . 6 the console.log (myURL is); . 7 . 8 . 9 const = myURL is new new the URL (' HTTPS: // User: [email protected]: 8080 / P / A / T / H = Query String the hash # '? ); 10 the console.log (myURL is);
1 URL { 2 href: 3 'https://user:[email protected]:8080/p/a/t/h?query=string#hash', 4 origin: 'https://sub.itlike.com:8080', 5 protocol: 'https:', 6 username: 'user', 7 password: 'pass', 8 host: 'sub.itlike.com:8080', 9 hostname: 'sub.itlike.com', 10 port: '8080', 11 pathname: '/p/a/t/h', 12 search: '?query=string', 13 searchParams: URLSearchParams { 'query' => 'string' }, 14 hash: '#hash' 15 }
. 1 const = the require HTTP ( "HTTP" ); 2 . 3 http.createServer ((REQ, RES) => { . 4 the console.log (res.headersSent 'response header has been sent':? 'Response header is not transmitted' ); 5 // set the implicit response header . 6 res.setHeader ( 'the Content-the type', 'text / HTML; charset = UTF-. 8' ); . 7 res.writeHead (200 is, 'OK' ); . 8 res.write ( '< h1 of> the Hello, ItLike </ h1 of> ' ); . 9 res.write (' <h1 of> the Hello, ItLike </ h1 of> ' ); 10 res.write (' <h1 of> the Hello, ItLike </ h1 of> ' ); . 11 res.write ( '<h1>Hello, ItLike</h1>'); 12 //The response to the end 13 is res.end ( 'lift up INSTITUTE Division </ h1 of> <h1 of>' ); 14 the console.log (res.headersSent 'response header has been sent':? 'Response header is not transmitted' ); 15 }) .listen (3000, '127.0.0.1', () => { 16 the console.log ( 'server is started ~' ) 17 });