添加letsencrypt证书到nodejs http服务器?
比方说,我有一个简单的nodejs http服务器,如下所述,还letsencrypt证书准备在/etc/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!'); });
您需要使用https
模块。 这里是一个如何configuration你的服务器的例子:
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);