The http2.createServer() is a Node.js method used to create an HTTP/2 server with optional configuration and a request handler. It belongs to the node:http2 module and provides a simple, familiar way to handle HTTP/2 requests.
- It creates an HTTP/2 server instance.
- It follows a compatibility API similar to HTTP/1.1.
- For secure or mixed protocol support,
createSecureServer()is preferred.
Syntax:
http2.createServer([options][, onRequestHandler])Where
options(optional): An object to configure server settings such as HTTP/2 session behavior.onRequestHandler(optional): A callback function(request, response)that runs when a request is received.
Return Value: An Http2Server instance that can listen for connections using .listen().
Example 1:
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'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
server.on('stream', (stream, requestHeaders) => {
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.write('hello ');
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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 closed");
})
});
Output:
status : 200
Received: hello
Received: world
client closed
server closed
Example 2:
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'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
server.on('stream', (stream, requestHeaders) => {
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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 closed");
})
});
Output:
Received: world
client closed
server closed
Use Cases of http2.createServer()
- It is used to create simple HTTP/2 servers without HTTPS.
- It is used to build APIs that take advantage of HTTP/2 features.
- It is used for testing or prototyping HTTP/2 applications.