lukeed / sirv

An optimized middleware & CLI application for serving static files~!
MIT License
1.07k stars 58 forks source link

Is there a way to run an adhoc localhost HTTPS server? #69

Closed mightymercado closed 4 years ago

mightymercado commented 4 years ago

Title says it all.

lukeed commented 4 years ago

No it doesnt. Please explain what you're trying to do

mightymercado commented 4 years ago

Normally, sirv serves your static files using HTTP. Is there a way to serve it using https?

lukeed commented 4 years ago

If you mean sirv-cli (the CLI), then refer here: https://github.com/lukeed/sirv/tree/master/packages/sirv-cli#http2

If you mean sirv itself (the middleware), then no, because it's a handler/middleware function that runs in your existing server. The simplest thing you can do is:

const { createServer } = require('https');
const { readFileSync } = require('fs');

const options = {
  key: readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: readFileSync('test/fixtures/keys/agent2-cert.pem')
};

const handler = require('sirv')('public');
createServer(options, handler).listen(PORT);

The sirv-cli package effectively does this, but better, through the http2 module.

Hope that helps~!

mightymercado commented 4 years ago

Thanks! It worked. Had to update sirv-cli to 1.0.1 because my-svelte-project template was using an older version. It would be great if there was an -adhoc option that does the certificates automatically!

lukeed commented 4 years ago

Ah ya, it only exists in 1.x -- PRs are open to update those templates.

Will not auto scaffold certificates because there are 100 tools that already do that, and it's very system-specific