當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。