
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@httptoolkit/httpolyglot
Advanced tools
Serve http and https connections over the same port with node.js
Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)
A module for serving HTTP, HTTPS and HTTP/2 connections, all over the same port.
Forked from the original httpolyglot to fix various issues required for HTTP Toolkit, including:
tlsClientError: https://github.com/mscdex/httpolyglot/pull/11clientError events (https://github.com/mscdex/httpolyglot/issues/13)server.on(x, ...) to hear about x from all internal servers - HTTP/2, HTTP/1, TLS and net)npm install @httptoolkit/httpolyglot
import * as httpolyglot from '@httptoolkit/httpolyglot';
import * as fs from 'fs';
httpolyglot.createServer({
tls: {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}
}, function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end((req.socket.encrypted ? 'HTTPS' : 'HTTP') + ' Connection!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
import * as httpolyglot from '@httptoolkit/httpolyglot';
import * as fs from 'fs';
httpolyglot.createServer({
tls: {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
}
}, function(req, res) {
if (!req.socket.encrypted) {
res.writeHead(301, { 'Location': 'https://localhost:9000' });
return res.end();
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome, HTTPS user!');
}).listen(9000, 'localhost', function() {
console.log('httpolyglot server listening on port 9000');
// visit http://localhost:9000 and https://localhost:9000 in your browser ...
});
If no config is provided, this server handles HTTP/1 & HTTP/2 in plain text on the same port.
If a config is provided, it can contain:
tls - Either TLS configuration options for tls.createServer, or an existing tls.Server instance. Setting this option enables TLS, so that HTTP/1 & HTTP/2 are accepted over both plain-text & encrypted (HTTPS) connections on the same port. If configuration options are provided, Httpolyglot will handle TLS automatically. If a server is provided, the connection will be passed to it (by emitting the connection event) and Httpolyglot will wait for the server to emit secureConnection (the default TLS server event) to handle the content within.socks - A net.Server instance, which will handle any incoming SOCKS connections. If this is provided, SOCKSv4 and SOCKSv5 connections will be emitted as connection events on this server. If not, all SOCKS connections will be treated like any other unknown protocol.unknownProtocol - A net.Server instance, which will handle any unknown protocols. If this is provided, unrecognized protocols will be emitted as connection events on this server. If it's not provided, these connections will be passed to the HTTP server by default, which will typically result in a clientError event and a 400 HTTP response being sent to the client.TLS, HTTP, HTTP/2, SOCKS and other connections are easy to distinguish based on the first byte sent by clients trying to connect. See this comment for more information.
FAQs
Serve http and https connections over the same port with node.js
The npm package @httptoolkit/httpolyglot receives a total of 207,082 weekly downloads. As such, @httptoolkit/httpolyglot popularity was classified as popular.
We found that @httptoolkit/httpolyglot demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.