node.js学习笔记之HTTP---发送服务器端响应流

/*使用http.ServerResponse对象的writeHead方法设置响应头信息*/

var http = require('http');
var server = http.createServer(function (req, res) {

    if(req.url !== '/favicon.ico'){
        res.writeHead(200,{'Content-Type':'text/plain','Access-Control-Allow-Origin':'http://localhost'});
        /!*res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});*!/
        /!*res.setHeader('Content-Type','text/plain');
        res.setHeader('Access-Control-Allow-Origin','http://localhost');*!/
        res.write('您好');
    }
    res.end();
}).listen(3000,'localhost');

html5文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>使用ajax获取node.js服务端数据</title>
</head>
<body>
<input type="button" id="butten" value="获取数据" οnclick="GetData()" />
<div id="div">

</div>
<script type="text/javascript">
    function GetData() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://localhost:3000/", true);
        xhr.onreadystatechange = function() {
            if(xhr.readyState == 4) {
                if(xhr.status == 200) {
                    document.getElementById("div").innerHTML = xhr.responseText;
                }
            }
        }
        xhr.send(null);
    }
</script>
</body>

</html>




/*http.ServerResponse对象具有一个headersSent属性,当响应头发出时,该属性值为true,当响应头未发出时,该属性为false*/
var http = require('http');
var server = http.createServer(function (req, res) {
    if(req.url !== '/favicon.ico'){
        if(res.headersSent)
            console.log('响应头已发出');
        else
            console.log('响应头未发出');
        res.writeHead(200,{'Content-type':'text/html'});
        if(res.headersSent)
            console.log('响应头已发出');
        else
            console.log('响应头未发出');
        res.write('<html><head><meta charset="utf-8"/></head>');
        res.write('您好');
    }
    /!*
    *发送响应头的时机:
    *       1、在writeHead方法被调用时
    *       2、使用setHeader方法时,当write方法被第一次调用时发送响应头
    *结果为:
    * 响应头未发出
      响应头已发出
     *!/
    res.end();
}).listen(3000,'localhost');


/*
* node.js的http.ServerResponse对象的write方法返回一个布尔类型的返回值,当数据直接发送到操作系统内核缓冲区时返回true
*               当数据缓存到内存中时返回false
*   可以使用http.ServerResponse对象的setTimeout方法设置相应超时时间
*           response.setTimeout(msecs,[callback])
*           msecs为一个整数,设置超时时间,单位为毫秒,回调函数没有参数。也可以不写回掉函数,
*                           通过监听http.ServerResponse对象的timeout事件并指定事件回掉函数方法完成响应超时所需要的处理
* */

/*setTimeout方法使用示例*/

var http = require('http');
var server = http.createServer(function (req, res) {
    if(req.url != '/favicon.ico'){
        res.setTimeout(1000);
        //如果在setTimeout函数中写了回调函数,或者指定了timeout的事件监听函数,
        //则相应超时使不会关闭与客户端连接的HTTP客户端socket端口
        //所以在本例中客户端仍然会收到‘您好’两字
        res.on('timeout',function () {
            console.log('响应超时');
        });
        setTimeout(function () {
            res.setHeader('Content-Type','text/html');
            res.write('<html><head><meta charset="utf-8"/></head>');
            res.write('您好');
            res.end();
        },2000);
    }
}).listen(3000);


/*
* 在http.ServerResponse对象的end方法被调用前,如果连接中断,将触发http.ServerResponse对象的close事件,
*
* */

/*http.ServerResponse对象的close事件使用示例*/

var http = require('http');
var server = http.createServer(function (req, res) {
    if(req.url != '/favicon.ico'){

        setTimeout(function () {
            //5秒后向客户端发送 您好
            //打开浏览器向服务器发送请求,然后在5秒内关闭页面,服务器就会打印 您好 二字
            res.setHeader('Content-Type','text/html');
            res.write('<html><head><meta charset="utf-8"/></head>');
            res.write('您好');
            res.end();
        },5000);

        res.on('close',function () {
            console.log('连接已被中断');
        })
    }
}).listen(3000);



发布了181 篇原创文章 · 获赞 36 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章