createServer
external fun createServer( onRequestHandler: (request: Http2ServerRequest, response: Http2ServerResponse<*>) -> Unit = definedExternally): Http2Server<*, *, *, *>(source)
Returns a net.Server
instance that creates and manages Http2Session
instances.
Since there are no browsers known that support unencrypted HTTP/2, the use of {@link createSecureServer} is necessary when communicating with browser clients.
import http2 from 'node:http2';
// Create an unencrypted HTTP/2 server.
// Since there are no browsers known that support
// unencrypted HTTP/2, the use of `http2.createSecureServer()`
// is necessary when communicating with browser clients.
const server = http2.createServer();
server.on('stream', (stream, headers) => {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
stream.end('<h1>Hello World</h1>');
});
server.listen(8000);
Content copied to clipboard
Since
v8.4.0
Parameters
onRequestHandler
See Compatibility API
external fun <Http1Request : IncomingMessage, Http1Response : ServerResponse<*>, Http2Request : Http2ServerRequest, Http2Response : Http2ServerResponse<*>> createServer( options: ServerOptions<Http1Request, Http1Response, Http2Request, Http2Response>, onRequestHandler: (request: Http2Request, response: Http2Response) -> Unit = definedExternally): Http2Server<Http1Request, Http1Response, Http2Request, Http2Response>(source)