📜  Node.js response.writeContinue() 方法

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

Node.js response.writeContinue() 方法

response.writeContinue()在 v0.3.0 中添加)方法是“ http ”模块的内置应用程序编程接口,它向客户端发送HTTP/1.1 100 Continue 消息,指示应该发送请求正文。查看服务器上的“ checkContinue ”事件。即使没有附加侦听器,也会在内部调用response.writeContinue()

为了得到响应和正确的结果,我们需要导入' http '模块。

进口:

const http = require('http');

句法:

response.writeContinue();

参数:此方法不接受任何参数。

返回值:不返回任何值,而是向客户端发送HTTP/1.1 100 Continue 消息,表示应该发送请求体。

下面的示例说明了 Node.js 中response.writeContinue()方法的使用。

示例 1:没有response.writeContinue()方法的程序。

文件名:index.js

// Node.js program to demonstrate the 
// response.writeContinue() Method
  
// Importing http module
var http = require('http');
  
// Setting up PORT
const PORT = process.env.PORT || 3000;
  
// Creating http Server
var httpServer = http.createServer(function(request, response){
  
  // Writing string data
  response.write("Heyy geeksforgeeks ", 'utf8', () => {
      console.log("Writing Data...");
  });
  
  // Defining Buffer 'Hello world'
  const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
  
  // Writing the buffer data.
  response.write(buf, 'utf8', () => {
     console.log("Writing Buffer Data...");
  });
  
  // Prints Output on the browser in response
  response.end(' ok');
});
  
// Listening to http Server
httpServer.listen(PORT, () => {
   console.log("Server is running at port 3000...");
});

输出:

现在在浏览器中运行http://localhost:3000/

示例 2:使用 response.writeContinue()方法。

文件名:index.js

// Node.js program to demonstrate the 
// response.writeContinue() Method
  
// Importing http module
var http = require('http');
  
// Setting up PORT
const PORT = process.env.PORT || 3000;
  
// Creating http Server
var httpServer = http.createServer(function(request, response){
  
  // Writing string data
  response.write("Heyy geeksforgeeks ", 'utf8', () => {
      console.log("Writing Data...");  
  });
  
  // Using response.writeContinue() method
  response.writeContinue();
  
  // Defining Buffer 'Hello world'
  const buf = Buffer.alloc(11, 'aGVsbG8gd29ybGQ=', 'base64');
  
  // Writing the buffer data.
  response.write(buf, 'utf8', () => {
      console.log("Writing Buffer Data...");
  });
  
  // Prints Output on the browser in response
  response.end(' ok');
});
  
// Listening to http Server
httpServer.listen(PORT, () => {
   console.log("Server is running at port 3000...");
});

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

node index.js

输出:

参考: https://nodejs.org/api/http.html#http_response_writecontinue