https-server.js 946 B

1234567891011121314151617181920212223242526272829303132
  1. const https = require('https');
  2. const selfsigned = require('../');
  3. async function main() {
  4. // Generate a self-signed certificate
  5. const pems = await selfsigned.generate([
  6. { name: 'commonName', value: 'localhost' }
  7. ], {
  8. days: 365,
  9. keySize: 2048,
  10. algorithm: 'sha256'
  11. });
  12. // Create HTTPS server with the generated certificate
  13. const server = https.createServer({
  14. key: pems.private,
  15. cert: pems.cert
  16. }, (req, res) => {
  17. res.writeHead(200, { 'Content-Type': 'text/plain' });
  18. res.end('Hello from self-signed HTTPS server!\n');
  19. });
  20. const port = 3443;
  21. server.listen(port, () => {
  22. console.log(`HTTPS server running at https://localhost:${port}/`);
  23. console.log('Certificate fingerprint:', pems.fingerprint);
  24. console.log('\nNote: Your browser will warn about the self-signed certificate.');
  25. console.log('Test with: curl -k https://localhost:' + port);
  26. });
  27. }
  28. main().catch(console.error);