Node.js request.writableEnded 属性
request.writableEnded (在 v12.9.0 中添加)属性是“ http ”模块的内置属性,它在request.end()被调用后返回 true。此属性不指示数据是否已刷新,为此使用request.writableFinished代替。
为了得到响应和正确的结果,我们需要导入' http '模块。
句法:
const http = require('http');
句法:
request.writableEnded
参数:此属性不接受任何参数。
返回值< Boolean > : request.end()被调用后返回true。
下面的例子说明了在 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