Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including express-https-provider with all npm packages installed. Try it out:

var expressHttpsProvider = require("express-https-provider")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

express-https-provider v1.0.12

express-https-provider

Downloads Version License

Description

This is a JavaScript library that helps you to run development express server with trusted ssl certificate easily or get a valid certificate to run a dev server using different from express library. Forget about chrome warnings, errors and etc. Please, never use this library on production server.

Attention! Your server will be serving both at https://yourapp.name and http://localhost, but all request will be redirected to the first one by default.

Browser screenshot of trusted certificate on local machine (node.js)

Buy Me A Coffee

Installation

You can use yarn or npm to install this package.

Yarn

yarn add --dev express-https-provider

npm

npm install --save-dev express-https-provider

Usage

const provider = require('express-https-provider')()

provider
  .modifyApp((app, state) => {
    app.get('/', (request, response) => {
      response.send('example')
    })
  })
  .done((http, https, state) => {
    console.log(`Serving at ${state.getServingLink()}`)
  })
  .run()

Documention

Methods

Note that modifyApp, modifyRedirect, done, disableRedirectToHttps and addCloseEventHandler return link to the object on which you call the method so you can combine it in chain.

const provider = require('express-https-provider')()

provider
    .disableRedirectToHttps()
    .modifyApp(app => {
      //
    })
    .modifyRedirect(redirect => {
      //
    })

modifyApp

Allow to set up your express server before it runs.

Arguments
  • app - This is an express server instance that process client requests. Use this object to set up your server.
  • state - AppState instance
const provider = require('express-https-provider')()

provider
    .modifyApp((app, state) => {
      app.get('/', (request, response) => {
        response.send(`Serving at ${state.getServingLink()}`)
      })
    })

modifyRedirect

Allow to set up custom logic on each request before it runs.

Arguments
  • redirect - This is an express server instance used to redirect each request to 'app' express instance.
  • state - AppState instance
const provider = require('express-https-provider')()

provider
    .modifyRedirect((redirect, state) => {
      redirect.all('*', (request, response, next) => {
        if (request.secure) {
          return next()
        }
        // do something
      })
    })

done

It is an event hook fired after server running.

Arguments
const provider = require('express-https-provider')()

provider
  .done((http, https, state) => {
    console.log(`Serving at ${state.getServingLink()}`)
    
    // As example
    http.close()
  })
  .run()

disableRedirectToHttps

Use this method if you don't want to auto redirect from http://localhost to https://yourapp.name.

No arguments
const provider = require('express-https-provider')()

provider
  .disableRedirectToHttps()
  .run()

addCloseEventHandler

It is an event hood fired before server stopping.

Arguments
const provider = require('express-https-provider')()

provider
  .addCloseEventHandler(state => {
    console.log(`Stop serving at ${state.getServingLink()}`)
  })
  .run()

run

This method runs express server.

If you don't want to run express server and just want to get a certificate to use it on your own you can use certificate method.

No arguments

Return promise

const provider = require('express-https-provider')()

provider.run()

You can await this method if you need to do it sync.

const provider = require('express-https-provider')()

const runProvider = async () => {
  await provider.run()
  // Server was run but done hook may not be fired yet!
}

certificate

Provide trusted SSL certificate without server running.

Attention! Chainable method like modifyApp and others actions do NOT make any effect on it.

No arguments

Return promise

const provider = require('express-https-provider')()

provider.certificate()
    .then(certificate => {
      const {cert, key} = certificate
      // do whatever you need
    })
    .catch(error => {
      console.error('Whoops, something went wrong :(')
    })

AppState

Methods

getServingLink
No arguments

Return string

The method returns secure URL your server serving at. If 443 port is free it returns https://yourapp.name, otherwise it do https://yourapp.name:${port} where ${port} is automatically selected free port. If you need to get a port number for secure URL you can use getHttpsPort method.

getNotSecureServingLink

Return string

The method returns not secure URL your server serving at. If 80 port is free it returns http://localhost, otherwise it do http://localhost:${port} where ${port} is automatically selected free port. If you need to get a port number for not secure URL you can use getHttpPort method.

getHttpPort
No arguments

Return int

The method returns port your http server listening on. If 80 port was free it return 80, otherwise automatically selected free port.

getHttpsPort
No arguments

Return int

The method returns port your https server listening on. If 443 port was free it return 443, otherwise automatically selected free port.

getHostname
No arguments

Return string

The method returns virtual host domain name your https server serving at. Currently is yourapp.name.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free