Node.jsのhttpsサーバを立ててみます。テスト用です。
オレオレ証明書を作ります。
$ openssl genrsa -out key.pem 1024 $ openssl req -new -key key.pem -out csr.pem You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:JP State or Province Name (full name) [Some-State]:Tokyo Locality Name (eg, city) []:Shinagawa Organization Name (eg, company) [Internet Widgits Pty Ltd]:Lucky And Happy Ltd Organizational Unit Name (eg, section) []: Common Name (eg, YOUR name) []:Tomoyuki Inoue Email Address []:メールアドレス Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: $ openssl x509 -req -in csr.pem -signkey key.pem -out cert.pem
key.pemとcert.pemをserver.jsと同じディレクトリに保存しておきます。
https_sample.js
/** ++*[https_sample.js]*++ - node.jsでオレオレhttpsサーバのサンプルプログラム
*
* @version 0.0.1
* @author TOmoyuki Inoue
*/
var https = require('https');
var fs = require('fs');
// 証明書のファイルを指定します
var options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
// ポート3000でサーバを生成します
https.createServer(options, function(req, res) {
res.writeHead(200);
res.end('Hello World!\n');
}).listen(3000);
console.log('Started server https://localhost:3000/');
サーバを起動します。
$ node https_sample.js Started server https://localhost:3000/
https://localhost:3000/ にアクセス。
Safariの場合
信頼されていない証明書の警告が表示されます。
証明書を表示してみます。
続けるボタンで表示されます。
Chromeの場合
アドレスバーの鍵アイコンをクリックして、証明書情報を表示してみます。
[このまま続行]ボタンで表示されます。






0 件のコメント:
コメントを投稿