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:沒有響應的程序。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...");
});
輸出:
Output: In-Console
Server is running at port 3000…
Writing Data…
Writing Buffer Data…
現在在瀏覽器中運行http://localhost:3000 /。
Output: In-Browser
Heyy geeksforgeeks hello world ok
範例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
輸出:
Output: In-Console
Server is running at port 3000…
Writing Data…
Writing Buffer Data…
參考: https://nodejs.org/api/http.html#http_response_writecontinue
相關用法
- Node.js process.nextTick()用法及代碼示例
- Node.js GM solarize()用法及代碼示例
- Node.js MySQL Max()用法及代碼示例
- Lodash _.method()用法及代碼示例
- Node.js Http2ServerRequest.method用法及代碼示例
- Node.js http.IncomingMessage.method用法及代碼示例
- Collect.js toArray()用法及代碼示例
- Javascript RegExp toString()用法及代碼示例
- Tensorflow.js tf.Sequential.evaluate()用法及代碼示例
- Node.js URLSearchParams.has()用法及代碼示例
- JavaScript Math cosh()用法及代碼示例
- Node.js hmac.update()用法及代碼示例
- jQWidgets jqxFormattedInput val()用法及代碼示例
- HTML DOM isEqualNode()用法及代碼示例
- JavaScript Date toLocaleTimeString()用法及代碼示例
- Tensorflow.js tf.Tensor.buffer()用法及代碼示例
注:本文由純淨天空篩選整理自vikas_g大神的英文原創作品 Node.js response.writeContinue() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。