zoukankan      html  css  js  c++  java
  • node.js 基础学习笔记3 -http

      http模块,其中封装了一个高效的HTTP服务器和一个建议的HTTP客户端

      http.server是一个基于事件的HTTP服务器

      http.request则是一个HTTP客户端工具,用户向服务器发送请求。

     HTTP服务器

       (1)http.Server实现,提供了一套封装级别很低的API,仅仅是流控制和简单的解析。

        request当客户端请求到来时,该事件被触发,提供两个参数,分别是http.ServerRequest和http.ServerResponse的实例,表示请求和相应信息。 

    var http=require('http');
    var server=new http.Server();
    server.on('request',function(req,res){
        res.writeHead(200,{'Content-Type':'text/html'});
        res.write('<h1>we are hello</h1>');
        res.end("<p>I'm request</p>");
    });
    server.listen(3000);

        http提供了一个捷径,http.createServer([requestListner]) 

    var http=require('http')
    
    var server=http.createServer(function(req,res){
        res.writeHead(200,{'Content-Type':'text/html'});
        res.write('<h1>Node.js</h1>');
        res.end('<p>this use createServer</p>');
    });
    
    server.listen(3000);
    server.on('close',function(){
        console.log('server is close');
    })
    console.log('HTTP server is listening at port 3000.');

      

      http.ServerResponse

        它是由http.Server的response事件发送的。

      主要有三个函数

        response.writeHead(statusCode,[headers])

        response.write(data,[encoding])

        response.end(data,[endcoding]) ,该函数必须调用一次,否则客户端永远处于等待状态。

        

  • 相关阅读:
    JSON连载java目的
    2014百度之星预赛(第二场)——Best Financing
    推断值的数组
    Codeforces 437E The Child and Polygon(间隔DP)
    ruby简单的基本 3
    定义和实现二叉树
    C++11并行编程-条件变量(condition_variable)详细说明
    【Bootstrap】自己主动去适应PC、平面、手机Bootstrap网格系统
    使用代码自定义UIView注意一二三
    关于 android 中 postDelayed方法的讲解
  • 原文地址:https://www.cnblogs.com/xianrongbin/p/4783738.html
Copyright © 2011-2022 走看看