LoginSignup
3
5

More than 5 years have passed since last update.

いつも書くNode.jsでhttpサーバを立てるテンプレート(オレオレhttps含む)

Last updated at Posted at 2017-09-16

いつも書くNode.jsのサーバまわりのテンプレートを、自分のメモのためだけに…

その後…

app.js
"use strict";                                   // 厳格モードにする

// express を使う
var express = require("express");               // express モジュールを使う
var app = express();                            // express アプリ
app.use(express.static(__dirname));             // ルートの静的ファイルへのアクセス許可
app.get("/", function(req, res) {               // ルートへのアクセス要求があったら
  res.sendFile(__dirname + "/index.html");      // index.html を配信
});

// http サーバ
var http = require("http").Server(app);         // http サーバを立てる
http.listen(80);                                // 80番ポートで待つ

// https サーバ
var fs = require("fs");                         // fs モジュールを使う
var opt = {                                     // SSL 認証のパラメータ
  key:  fs.readFileSync("server.key"),          // 秘密鍵
  cert: fs.readFileSync("server.crt"),          // 証明書
  //passphrase: "password",                     // パスワードを設定した場合
};
var https = require("https").Server(opt, app);  // https サーバを立てる
https.listen(443);                              // 443番ポートで待つ

すみません。ただそれだけです。

3
5
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
3
5