Code Monkey home page Code Monkey logo

node-localhost-https's Introduction

Node localhost HTTPS

This script automatically issues localhost HTTPS certificates using mkcert.

Installation & Usage

Use --experimental-network-imports with Node for HTTP(S) ESM URL support:

https://nodejs.org/api/esm.html#https-and-http-imports

import http from 'https';
import https from 'https';
import certify from 'https://tomashubelbauer.github.io/node-localhost-https/index.js';

/** @type {string | http.ServerOptions} */
let options;

const stateMessages = {
  read: 'Reading certificate files off the storage if any…',
  touch: 'Checking if mkcert is already available in storage…',
  version: 'Fetching the latest version of mkcert available…',
  redirect: 'Obtaining the direct mkcert download address…',
  download: 'Downloading the latest mkcert version available…',
  write: 'Storing the downloaded mkcert binary in the storage…',
  mod: 'Making mkcert executable in order to invoke it…',
  run: 'Generating localhost certificates using mkcert…',
};

http
  .createServer((request, response) => {
    if (request.method !== 'GET') {
      response.statusCode = 500;
      response.statusMessage = 'Cannot upgrade non-GET requests to HTTPS!';
      response.end();
      return;
    }

    const message = stateMessages[options];
    if (message) {
      response.writeHead(302, { Location: 'https://localhost' + request.url });
    }

    response.end(message);
  })
  .listen(80, () => console.log('http://localhost'))
  ;

for await (const state of certify()) {
  options = state;
}

if (typeof options === 'string') {
  throw new Error(options);
}

https
  .createServer(options, async (request, response) => { })
  .lister(443, () => console.log('https://localhost'))
  ;

This will create localhost.pem and localhost-key.pem in the working directory. It is recommended that you ignore these certificates in version control.

mkcert executable will be placed to the working directory. You can safely delete it and it will get downloaded again if needed.

You should run mkcert -install to place the certificate in all trust stores. To make Firefox trust it, you need to run brew install nss first (macOS).

To make other devices trust it, read on at https://github.com/FiloSottile/mkcert#installing-the-ca-on-other-systems

You might want to implement logic which in the HTTP to HTTPS redirect offers the option to download the certificate for manual installation to the trust store or automatically places it there using mkcert -install before doing the redirect.

node-localhost-https's People

Contributors

tomashubelbauer avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.