47
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Node.js Express で HTTPSを利用するパターン

Last updated at Posted at 2017-05-07

Node.js Expressフレームワークで、HTTPSサーバーを起動する方法の忘備録です。

Express HTTPサーバーを起動するパターン

// Expressフレームワーク
var express = require('express');
var app = express();

// ルート設定
app.get('/rest', function (req, res) {
  res.send('Hello World!');
});

// イベント待機 
app.listen(3000);

Express HTTPSサーバーを起動するパターン

事前準備として、DNS名を取得して証明書を取得しておきます。HTTPSサーバーと通常のHTTPサーバーとの違いは、HTTPSサーバー起動の部分です。

// Expressフレームワーク                                                                           
var express = require('express');
var app = express();

// HTTPSサーバー起動                                                                               
var fs = require('fs');
var https = require('https');
var options = {
  key:  fs.readFileSync('../lets_encript.key'),
  cert: fs.readFileSync('../lets_encript_fullchain.crt')
};
var server = https.createServer(options,app);

// ルート設定                                                                                      
app.get('/rest', function (req, res) {
    res.writeHead(200);
    res.end("Hello World.");
});

// イベント待機                                                                                    
server.listen(3000);

参考資料

47
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?