The http2ServerRequest.complete property in Node.js indicates whether the entire HTTP/2 request has been successfully received. It returns a boolean value that helps determine if the request data was fully transmitted.
- It indicates whether the complete request has been received.
- It helps detect incomplete or interrupted requests.
- It is useful for validating request processing before handling data.
Syntax:
request.completeWhere:
request: An instance of theHttp2ServerRequestobject.complete: A read-only boolean property that indicates whether the entire HTTP/2 request has been received.
Return Value: This method returns true if and only if this request has been completed, aborted, or destroyed or not.Â
Example 1:
// Node.js program to demonstrate the
// Http2ServerRequest.complete method
const http2 = require('http2');
const fs = require('fs');
// Private key and public certificate for access
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('public-cert.pem'),
};
// Request and response handler
const http2Handlers = (request, response) => {
// Checking if the request has been
// completed, aborted, or destroyed.
// by using request.complete method
const auth = request.complete;
if(auth)
console.log("request has been completed,"
+ " aborted, or destroyed")
else
console.log("request has not been completed,"
+ " aborted, or destroyed")
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(
options, http2Handlers);
server.on('stream', (stream, requestHeaders) => {
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.write('hello ');
// Getting all information of this http2stream object
// by using state method
const status = stream.state;
stream.end("priority weight : " + status.weight);
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server destroyed");
})
});
server.listen(8000);
// Creating and initializing client
// by using tls.connect() method
const client = http2.connect(
'http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
req.on('response', (responseHeaders) => {
console.log("status : "
+ responseHeaders[":status"]);
});
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client destroyed");
})
});
Output:
request has not been completed, aborted, or destroyed
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerRequest.complete method
const http2 = require('http2');
const fs = require('fs');
// Private key and public certificate for access
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('public-cert.pem'),
};
// Request and response handler
const http2Handlers = (request, response) => {
// Checking if the request has been
// completed, aborted, or destroyed.
// by using request.complete method
const auth = request.complete;
if(auth)
response.end("request has been completed,"
+ " aborted, or destroyed")
else
response.end("request has not been completed,"
+ " aborted, or destroyed")
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(
options, http2Handlers);
server.on('stream', (stream, requestHeaders) => {
// Getting all information of this http2stream object
// by using state method
const status = stream.state;
stream.end("The sum weight of all Http2Stream : " +
status.sumDependencyWeight);
// stopping the server
// by using the close() method
server.close(()=>{
console.log("server destroyed");
})
});
server.listen(8000);
// creating and initializing client
// by using tls.connect() method
const client = http2.connect('http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client destroyed");
})
});
Output:
Received: request has not been completed, aborted, or destroyed
client destroyed
server destroyed
Use Cases of http2ServerRequest.complete
- Checking whether the request was fully received.
- Detecting incomplete or interrupted requests.
- Validating request completion before processing data.
- Improving error handling for unfinished requests.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_request_complete