1. 需要openssl的支持, openssl本身不提供windows的安装程序,可以按照如下的步骤进行安装:


How-to Install OpenSSL on Windows 7

Download and run the Cygwin installer from their web sit OpenSSL is not one of that packages that gets installed by default with Cygwin. The important part of install is choosing OpenSSL as one of the packages you install, because that package is not selected by default. You do this by searching for "openssl" on the "Select Packages" step, expanding "Net" option, clicking on the "Skip" image so that a version shows, and clicking the "Next" button. Use the image below as a reference. [more]

Node.js中的HTTPS示例_node.js



​安装完后,在cygwin的bin目录下就可以找到openssl.exe. ​

  1. ​写如下的node.js文件。 ​


var https = require('https'),

pem = require('pem');


pem.config({

pathOpenSSL: 'C:\\cygwin64\\bin\\openssl.exe'

});


pem.createCertificate({days:1, selfSigned:true}, function(err, keys){

https.createServer({key: keys.serviceKey, cert: keys.certificate}, function(req, res){

res.end('o hai!')

}).listen(843);

});



 


const https = require('https');

const fs = require('fs');


const options = {

key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),

cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')

};


https.createServer(options, (req, res) => {

res.writeHead(200);

res.end('hello world\n');

}).listen(8000);



或者



const https = require('https'); 



const fs = require('fs'); 




const options = { 



  pfx: fs.readFileSync('server.pfx') 



}; 




https.createServer(options, (req, res) => { 



  res.writeHead(200); 



  res.end('hello world\n'); 



}).listen(8000);