JSPM

@quoin/node-http-with-https

2.0.0-rc.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q27114F
  • License MIT

Simple nodejs server with HTTP that redirects to HTTPS

Package Exports

  • @quoin/node-http-with-https

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@quoin/node-http-with-https) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@quoin/node-http-with-https

Build Status

This library offers a very simple code to force HTTP traffic to go in HTTPS.

Usage

const fs = require('fs');
const httpWithHttps = require('@quoin/node-http-with-https');

const behindProxy = false;
const httpPort = 8080;
const httpsPort = 8443;
const httpsOptions = {
  key: fs.readFileSync('/path/to/your/file.key'),
  cert: fs.readFileSync('/path/to/your/file.cert'),
  requestCert: false,
  rejectUnauthorized: false
};
const app = express(); // This is your expressJS app.

const { httpServer, httpsServer } = httpWithHttps(httpsPort, behindProxy, httpsOptions, app);

httpServer.listen(httpPort, () => console.log(`HTTP on ${httpPort}.`));

httpsServer.listen(httpsPort, () => console.log(`HTTPS on ${httpsPort}.`));

where:

  • httpsPort: port that will be used to run the application on HTTPS.
  • behindProxy: Indicates if the server runs behind a proxy. This will prevent httpsPort to be appended to the hostname.
  • httpsOptions: HTTPS options to be passed to https.createServer().
  • app: the expressJS app to run.

Example

See fully working example.

To run the example:

git clone git@github.com:Quoin/node-http-with-https.git
cd node-http-with-https
npm install
npm start

You can then access to:

http://localhost:8080/
http://localhost:8080/test/with/path

You should get an error trying any other path, like:

http://localhost:8080/other

Both should redirect you to the HTTPS path. You will probably have a security warning because the certificate is self-signed.

See example/README.md to see how the self-signed certificate files were generated.