http.ClientRequest.end() 是 http 模塊中 ClientRequest 類的內置應用程序編程接口,用於完成請求的發送。如果身體的任何部分未發送。
用法:
const request.end([data[, encoding]][, callback])
參數:該方法將數據作為輸入。
返回值:這個方法隻返回一個回調函數。
範例1:
Javascript
// Node.js program to demonstrate the
// request.end() APi
// Importing http module
const http = require('http');
// Create an HTTP server
http.createServer((req, res) => {}).listen(3000, '127.0.0.1', () => {
// getting client request
const req = http.request({
port:3000,
host:'127.0.0.1',
});
req.setHeader('content-type', 'text/html');
// finishing sending the request
req.end()
// getting header
// by using getHeader() api
console.log("header:- " + req.getHeader('content-type'))
process.exit(0)
});
輸出:
範例2:
Javascript
// Node.js program to demonstrate the
// request.end() 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);
req.setHeader('Cookie', ['type=ninja', 'language=javascript']);
// getting header
// by using getHeader() api
const v = req.getHeader('Cookie');
// finishing sending the request
req.end()
// display the result
console.log("header:- " + v)
process.exit(0)
});
輸出:
參考:https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_request_end_data_encoding_callback
相關用法
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js http.ClientRequest.end() API。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。