给定一个SSL密钥和证书,如何创建HTTPS服务?
我发现以下示例。
这适用于节点v0.1.94-v0.3.1。server.setSecure()在新版本的节点中被删除。
server.setSecure()
直接从该来源:
const crypto = require('crypto'), fs = require("fs"), http = require("http"); var privateKey = fs.readFileSync('privatekey.pem').toString(); var certificate = fs.readFileSync('certificate.pem').toString(); var credentials = crypto.createCredentials({key: privateKey, cert: certificate}); var handler = function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }; var server = http.createServer(); server.setSecure(credentials); server.addListener("request", handler); server.listen(8000);