关于 node.js的request事件

          下面展示的是一个ajax一部提交的服务器端处理过程。node创建一个web服务器,并侦听8080端口。对于服务器,我们为其绑定了request事件,对于请求对象,我们为它绑定了data和end事件:

   var   http =  require(‘http’);

   var   querystring =  require('querystring');

   //侦听服务器的request事件

       http.createServer(function(req,res){

              var      postDate = ' ' ;

              req.setEncoding('utf8');

          //侦听请求的data的事件;

             req.on('data',function(chunk){

                    postData += chunk;

});//侦听请求的end事件

          req.on('end' , function(){

             res.end(postData); 

});         

}).listen(8080);

console.log('服务器启动完成');

posted @ 2017-07-20 20:37  小太阳8  阅读(1876)  评论(0编辑  收藏  举报