📜  Node.js http.ClientRequest.writableEnded API

📅  最后修改于: 2022-05-13 01:56:37.580000             🧑  作者: Mango

Node.js http.ClientRequest.writableEnded API

http.ClientRequest.writableEnded是 http 模块中ClientRequest类的内置应用程序编程接口,用于检查request.end()是否已被调用。

句法:

const request.writableEnded

参数:此 API 不接受任何参数作为参数。

返回值:当且仅当request.end() 已被调用时,此方法才返回true

示例 1:

index.js
// Node.js program to demonstrate the  
// request.writableEnded APi
  
// Importing http module
const http = require('http');
  
// Create an HTTP server
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('okay');
});
  
// Now that server is running
server.listen(3000, '127.0.0.1', () => {
  
  // make a request
  const options = {
    port: 3000,
    host: '127.0.0.1',
    headers: {
      'Connection': 'Upgrade',
      'Upgrade': 'websocket'
    }
  };
  
  // getting client request
  const req = http.request(options);
  
  // checking if the request.end() has been called
  // by using writableEnded api
  if(req.writableEnded)
  console.log('request.end() has been called')
  else
  console.log('request.end() has not been called')
  
  process.exit(0)
});


输出: