Skip to content

Instantly share code, notes, and snippets.

@jayu108
Last active January 1, 2021 03:12
Show Gist options
  • Save jayu108/e6ef147c0beb830ec27c673a46f61707 to your computer and use it in GitHub Desktop.
Save jayu108/e6ef147c0beb830ec27c673a46f61707 to your computer and use it in GitHub Desktop.
Ubuntu 18.04 LTS 서버에 Let's Encrypt 인증서 설치후, nodejs express 에서 , https 서버 만들기 (http 서버도 포함됨)
'use strict';
const fs = require("fs");
const http = require("http");
const https = require("https");
const express = require("express");
const app = express(); // https
const app2 = express(); // http
// yourdomain.com 은 실제로 사용중인 도메인으로 바꿔야함. -- Let's Encrypt 인증서 설치시 위치 사용.
const privateKey = fs.readFileSync("/etc/letsencrypt/live/yourdomain.com/privkey.pem", "utf8");
const certificate = fs.readFileSync("/etc/letsencrypt/live/yourdomain.com/cert.pem", "utf8")
const ca = fs.readFileSync("/etc/letsencrypt/live/yourdomain.com/chain.pem", "utf8")
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
const httpServer = http.createServer(app2);
const httpsServer = https.createServer(credentials, app);
// 80 port -- http
app2.get("/", (req, res) => {
console.log("------ http get / -----" + (new Date()).toLocaleString());
console.log("req.ip => " + req.ip);
console.log("req.hostname => " + req.hostname);
console.log(req.url);
console.log(req.originalUrl);
res.send("<h1>HTTP Server running on port 80</h1>");
})
// 3000 port -- https
app.get("/", (req, res) => {
console.log("------ https get / -----" + (new Date()).toLocaleString());
console.log("req.ip => " + req.ip);
console.log("req.hostname => " + req.hostname);
console.log(req.url);
console.log(req.originalUrl);
res.send("<h1>HTTPS Server running on port 3000</h1>");
})
httpServer.listen(80, () => {
console.log((new Date()).toLocaleString());
console.log('HTTP Server running on port 80');
})
httpsServer.listen(3000, ()=>{
console.log((new Date()).toLocaleString());
console.log(`HTTPS -- listening on port 3000 ...`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment