码迷,mamicode.com
首页 > Web开发 > 详细

发出Get请求的基本Web客户端

时间:2016-07-02 18:55:29      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

var http=require("http");
var message=[
    "HelloWorld",
    "From a basic Node.js server",
    "Take Luck"
];
http.createServer((req,res)=>{
    res.setHeader("Content-Type","text/html");
    res.writeHeader(200);
    res.write("<html><head><title>Simple Http Serever</title></head>");
    res.write("<body>");
    for(let i in message){
        res.write(`\n<h1><mark>${message[i]}</mark></h1></mark>`);
    }
    res.end("\n</body></html>");
    console.log("Look me ! I succeed ! ! !");
}).listen(8080);

var options={
    hostname:"www.soul.com",
    port:"8080"
};
function handleResponse(response){
    var serverData="";
    response.on("data",(chunk)=>{
        serverData+=chunk;
    });
    response.on("end",()=>{
        console.log(`Response Status:${response.statusCode}`);
        console.log(`Response Headers:${response.headers}`);
        console.log(serverData);
    });
}
http.get(options,(response)=>{
    handleResponse(response);
});

技术分享

发出Get请求的基本Web客户端

标签:

原文地址:http://www.cnblogs.com/realsoul/p/5635716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!