Unlock the full potential of modern networking with native HTTP/2 support, including multiplexing and server push.
Multiple requests over a single TCP connection.
Proactively send assets to the client.
Native support for SSL/TLS.
Create an HTTP/2 server using the helper function. Note that SSL certificates are required.
1import { createHttp2Server } from "canxjs";2import fs from "fs";34// HTTP/2 requires SSL/TLS5const server = createHttp2Server({6port: 3000,7key: fs.readFileSync("./cert/key.pem"),8cert: fs.readFileSync("./cert/cert.pem"),9allowHTTP1: true // Fallback to HTTP/1.110});1112server.get("/", (req, res) => {13return res.send("Hello form HTTP/2!");14});1516server.listen();
Push assets to the client before they are requested, reducing load times.
1// Server Push Example2server.get("/", (req, res) => {3// Push style.css automatically when index is requested4if (res.stream.pushAllowed) {5res.push("/style.css", {}, (err, stream) => {6stream.on('error', console.error);7stream.respond({ ':status': 200, 'content-type': 'text/css' });8stream.end('body { background: #000; }');9});10}1112return res.html('<html><head><link rel="stylesheet" href="/style.css"></head><body><h1>HTTP/2</h1></body></html>');13});