2017-03-07 5 views
1

下記のような単純なnodejs httpサーバーと、letsencryptの証明書が/etc/letsencryptに用意されているとします。 httpsに変更して証明書を追加するにはどうすればいいですか?letsencrypt証明書をnodejs httpサーバーに追加していますか?

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!'); 
}); 

答えて

1

あなたが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); 
+0

何key.pemとなしのcert.pemファイルはありません...他のフォルダ内のいくつかのPEMファイルはもあります。 – vidstige

+0

@vidstige、証明書のファイルは何ですか? –

+0

@vidstigeの場合、 'letsencrypt'を正しく使って証明書を生成していないでしょう。 [これをチェックしてください](https://gist.github.com/davestevens/c9e437afbb41c1d5c3ab) –

関連する問題