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


Node.js request.writableEnded用法及代碼示例


request.writableEnded(在v12.9.0中添加)屬性是‘http’模塊的內置屬性,該屬性在調用request.end()後返回true。此屬性不指示是否已刷新數據,而是為此用途request.writableFinished。

為了獲得響應和正確的結果,我們需要導入‘http’模塊。

用法:

const http = require('http');  

用法:

request.writableEnded 

參數:此屬性不接受任何參數。



返回值<布爾值>:請求後返回true.end()已被調用。

以下示例說明了Node.js中request.writableEnded屬性的用法。

例: 文件名:index.js

// Node.js program to demonstrate the  
// request.writableEnded 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 request = get(options);  
  
console.log("writableEnded:", request.writableEnded); 
  
request.end();  
  
console.log("writableEnded:", request.writableEnded); 
  
request.once('response', (res) => {  
  
    // Printing the requestrelated data  
    console.log("Status:", res.statusCode, res.statusMessage);  
    console.log("Writable:", request.socket.writable);  
    console.log("Readable:", request.socket.readable);  
      
    console.log("writableEnded:", request.writableEnded); 
  
    // Printing address and port after getting response  
    console.log(`IP address of geeksforgeeks is`, 
    ` ${request.socket.localAddress}.`);  
  
    console.log(`Its port is ${request.socket.localPort}.`);  
}); 

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

node index.js

輸出:

writableEnded:true

writableEnded:true

Status:301 Moved Permanently

Writable:true

Readable:true

writableEnded:true

IP address of geeksforgeeks is 192.168.43.207.

Its port is 64596

參考: https://nodejs.org/api/http.html#http_request_writableended

相關用法


注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js request.writableEnded Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。