zoukankan      html  css  js  c++  java
  • Node-Web模块

    创建服务端------------------------------------------------------
    
    var http = require('http');
    var fs = require('fs');
    var url = require('url');
     
     
    // 创建服务器
    http.createServer( function (request, response) {  
       // 解析请求,包括文件名
       var pathname = url.parse(request.url).pathname;
       
       // 输出请求的文件名
       console.log("Request for " + pathname + " received.");
       
       // 从文件系统中读取请求的文件内容
       fs.readFile(pathname.substr(1), function (err, data) {
          if (err) {
             console.log(err);
             // HTTP 状态码: 404 : NOT FOUND
             // Content Type: text/html
             response.writeHead(404, {'Content-Type': 'text/html'});
          }else{             
             // HTTP 状态码: 200 : OK
             // Content Type: text/html
             response.writeHead(200, {'Content-Type': 'text/html'});    
             
             // 响应文件内容
             response.write(data.toString());        
          }
          //  发送响应数据
          response.end();
       });   
    }).listen(8080);
     
    // 控制台会输出以下信息
    console.log('Server running at http://127.0.0.1:8080/');
    
    
    
    创建客户端------------------------------------------------------
    var http = require('http');
     
    // 用于请求的选项
    var options = {
       host: 'localhost',
       port: '8080',
       path: '/index.html'  
    };
     
    // 处理响应的回调函数
    var callback = function(response){
       // 不断更新数据
       var body = '';
       response.on('data', function(data) {
          body += data;
       });
       
       response.on('end', function() {
          // 数据接收完成
          console.log(body);
       });
    }
    // 向服务端发送请求
    var req = http.request(options, callback);
    req.end();
    
    
    
    
  • 相关阅读:
    vxlan 内核实现
    perf 系统调用
    quic NAT
    机器学习在ABR算法中的应用纵览
    清华最新AIOps案例:强化学习,降低网络传输延时
    内核gso
    怎么提高网络应用性能?让DPDK GRO和GSO来帮你!
    网卡VXLAN的offload技术介绍
    Matplotlib中的plt和ax画图的区别
    Task 3: 异常处理(2天)
  • 原文地址:https://www.cnblogs.com/csnd/p/12061857.html
Copyright © 2011-2022 走看看