当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js request.socket用法及代码示例


request.socket(在v0.3.0中添加)属性是‘http’模块的内置属性,它引用了基础套接字,大多数用户无法访问此属性。特别是,套接字不会发出“可读”事件,但是可以通过request.connection访问该套接字。此属性保证是<net.Socket>类的实例,该类是<stream.Duplex>的子类。

为了获得响应和正确的结果,我们需要导入‘http’模块。

const http = require('http'); 

用法:

request.socket

参数:此属性不接受任何参数。

返回值:它以对象的形式返回请求数据,该对象包含大量与套接字相关的数据。



  • <stream.Duplex>:<stream.Duplex>或双工流是同时实现可读和可写的流。

以下示例说明了Node.js中request.socket属性的使用。

范例1: 文件名:index.js

// Node.js program to demonstrate the  
// req.socket property  
  
// Using require to access http module  
const http = require('http'); 
  
// Requesting from google server 
const req = http.get({ host:'www.geeksforgeeks.org' }); 
  
// Ending the request 
req.end(); 
  
req.once('response', (res) => { 
  
    // Printing socket after getting response 
    console.log(req.socket); 
  
    // Printing address and port after 
    // getting response 
    console.log(`IP address of geeksforgeeks is  
            ${req.socket.localAddress}.`); 
              
    console.log(`Its Port is ${req.socket.localPort}.`); 
});

输出:

>> <ref *1> Socket{ connecting:false,

    _hadError:false,

    _parent:null,

    _host:‘www.geeeksforgeeks.org’… [Symbol(kBytesWritten)]:0 }

>> IP address is 192.168.43.207



>> Its port is 56933.

范例2:文件名:index.js

// Node.js program to demonstrate the  
// req.socket property  
  
// Using require to access http module  
const { get } = require('http'); 
  
// Setting host server url 
const options = { host:'www.geeksforgeeks.org' }; 
  
// Requesting from geeksforgeeks server 
const req = get(options); 
req.end(); 
  
req.once('response', (res) => { 
  
    // Printing the requestrelated data 
    console.log("Status:", res.statusCode,  
                        res.statusMessage); 
  
    console.log("Host:", req.socket._host); 
  
    console.log("Method:",  
        req.socket.parser.outgoing.method); 
  
    console.log("Parser Header:",  
            req.socket.parser.outgoing._header); 
  
    console.log("Writable:", req.socket.writable); 
    console.log("Readable:", req.socket.readable); 
      
    console.log("Http Header:",  
            req.socket._httpMessage._header); 
  
    if (req.socket._httpMessage._header ===  
            req.socket.parser.outgoing._header) { 
        console.log("Both headers are exactly same...") 
    } else { 
        console.log("Headers are not same...") 
    } 
  
    // Printing address and port after 
    // getting response 
    console.log(`IP address of geeksforgeeks is  
        ${req.socket.localAddress}.`); 
  
    console.log(`Its port is ${req.socket.localPort}.`); 
});

使用以下命令运行index.js文件:

node index.js

输出:

>> Status:301 Moved Permanently

>> Host:www.geeksforgeeks.org

>> Method:GET

>> Parser Header:GET / HTTP/1.1

    Host:www.geeksforgeeks.org

    Connection:close



>> Writable:true

>> Readable:true

>> Http Header:GET / HTTP/1.1

    Host:www.geeksforgeeks.org

    Connection:close

>> Both headers are exactly same…

>> IP address is 192.168.43.207

>> Its port is 57425.

参考: https://nodejs.org/api/http.html#http_request_socket




相关用法


注:本文由纯净天空筛选整理自vikas_g大神的英文原创作品 Node.js request.socket Property。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。