我有一个拥有SSL证书的域名。我的默认应用程序运行完美。现在我在另一个使用SSL的端口运行node.js应用程序,即https服务器。但是服务器没有运行。这是我的示例代码。
var http = require('http'),
https = require('https'),
fs = require('fs'),
express = require("express");
var options = {
key: fs.readFileSync("/etc/ssl/abc.com-key.pem",),
cert: fs.readFileSync("/etc/ssl/abc.com.pem")
};
var app = express(options);
var server = https.createServer(options, app).listen(MY_IP, 9002);
任何帮助?提前谢谢。
答案 0 :(得分:1)
这是一个例子 -
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);