Numerous Pancakes Munched

    @httptoolkit/httpolyglot
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.1 • Public • Published

    Httpolyglot Build Status Available on NPM

    Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)

    A module for serving http and https connections over the same port.

    Forked from the original httpolyglot to fix various issues required for HTTP Toolkit, including:

    Requirements

    Install

    npm install @httptoolkit/httpolyglot
    

    Examples

    • Simple usage:
    const httpolyglot = require('@httptoolkit/httpolyglot');
    const fs = require('fs');
    
    httpolyglot.createServer({
      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 ...
    });
    • Simple redirect of all http connections to https:
    const httpolyglot = require('@httptoolkit/httpolyglot');
    const fs = require('fs');
    
    httpolyglot.createServer({
      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 ...
    });

    API

    Exports

    • Server - A class similar to https.Server (except instances have setTimeout() from http.Server).

    • createServer(< object >tlsConfig[, < function >requestListener]) - Server - Creates and returns a new Server instance.

    How it Works

    TLS and HTTP connections are easy to distinguish based on the first byte sent by clients trying to connect. See this comment for more information.

    Install

    npm i @httptoolkit/httpolyglot

    DownloadsWeekly Downloads

    38,738

    Version

    2.0.1

    License

    none

    Unpacked Size

    24.2 kB

    Total Files

    7

    Last publish

    Collaborators

    • pimterry