http模块
1 const http = require('http'); 2 3 let server = http.createServer((req, res)=>{ 4 res.end('hello, world!'); 5 }); 6 7 server.listen(3000, '127.0.0.1', ()=>{ 8 console.log('服务器已经启动!'); 9 // 5秒后关闭服务器 10 setTimeout(()=>{ 11 // server.close(); 12 }, 5000); 13 }); 14 15 // 监听服务器的关闭 16 server.on('close', ()=>{ 17 console.log('服务器已经关闭!'); 18 }); 19 20 // 监听服务器发生错误 21 server.on('error', (e)=>{ 22 if(e.code === 'EADDRINUSE'){ 23 console.log('端口号被调用!'); 24 }else { 25 console.log('其它的错误', e.code); 26 } 27 }); 28 29 // 设置超时时间 30 server.setTimeout(1000, ()=>{ 31 console.log('设置超时时间为1s'); 32 }); 33 34 server.on('timeout', ()=>{ 35 // 超时要做什么操作 36 console.log('连接已经超时'); 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 // 方式1 2 3 const url = require('url'); 4 // 把字符串转成URL对象 5 const myURL = url.parse( 'https://user:pass@sub.itLike.com:8080/p/a/t/h?query=string#hash'); 6 console.log(myURL); 7 8 9 const myURL = new URL('https://user:pass@sub.itLike.com:8080/p/a/t/h?query=string#hash'); 10 console.log(myURL);
1 URL { 2 href: 3 'https://user:pass@sub.itlike.com: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 http = require('http'); 2 3 http.createServer((req, res)=>{ 4 console.log(res.headersSent ? '响应头已经发送' : '响应头未发送'); 5 // 设置隐式响应头 6 res.setHeader('Content-Type', 'text/html;charset=utf-8'); 7 res.writeHead(200, 'ok'); 8 res.write('<h1>Hello, ItLike</h1>'); 9 res.write('<h1>Hello, ItLike</h1>'); 10 res.write('<h1>Hello, ItLike</h1>'); 11 res.write('<h1>Hello, ItLike</h1>'); 12 // 本次响应结束 13 res.end('<h1>撩课学院</h1>'); 14 console.log(res.headersSent ? '响应头已经发送' : '响应头未发送'); 15 }).listen(3000, '127.0.0.1', ()=>{ 16 console.log('服务器已经启动~') 17 });