深入解析nodejs HTTP服务(3)

var http=require('http'); var messages=[ 'message1', 'message2', 'message3' ]; http.createServer(function(req,res){ res.setHeader('Content-Type','text/html'); res.writeHead(200); res.write('<html><head><title>HTTP Server</title></head>'); res.write('<body>'); for(var idx in messages){ res.write('\n<h1>'+messages[idx]+'</h1>'); } res.end('\n</body></html>'); }).listen(8080); var options={ hostname:'localhost', port:'8080' }; function handleResponse(response){ var serverData=''; response.on('data',function(chunk){ serverData+=chunk; }); response.on('end',function(){ console.log('response status: ',response.statusCode); console.log('response headers: ',response.headers); console.log(serverData); }); } http.request(options,function(response){ handleResponse(response); }).end();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjgzz.html