假设我有一个如下所述的简单nodejs http服务器,还有/etc/letsencrypt
中准备好的letsencrypt证书。如何更改为https并添加证书?
var http = require('http');
var app = require('express')();
app.get('/', function (req, res) {
res.send('Hello World!');
});
http.createServer(app).listen(3000, function () {
console.log('Started!');
});
答案 0 :(得分:2)
您需要使用https
模块。以下是如何配置服务器的示例:
const https = require('https');
const fs = require('fs');
function letsencryptOptions(domain) {
const path = '/etc/letsencrypt/live/';
return {
key: fs.readFileSync(path + domain + '/privkey.pem'),
cert: fs.readFileSync(path + domain + '/cert.pem'),
ca: fs.readFileSync(path + domain + '/chain.pem')
};
}
const options = letsencryptOptions('example.com');
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);