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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。