Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

melius

[Node.js] HTTPS 로컬 서버 구축 본문

Node.js

[Node.js] HTTPS 로컬 서버 구축

melius102 2020. 1. 29. 09:51

https://ko.wikipedia.org/wiki/HTTPS

 

HTTPS(Hypertext Transfer Protocol Secure)는 웹 프로토콜인 HTTP의 보안이 강화된 버전으로, 소켓 통신에서 일반 텍스트를 이용하는 대신에 SSL(Secure Sockets Layer) 프로토콜을 통해 세션 데이터를 암호화한다.

Node.js에서는 내장모듈인 https를 이용하여 로컬에서 서버구축이 가능하다.

 

1. OpenSSL 설치 및 인증서 발급

https://www.openssl.org/

https://wiki.openssl.org/index.php/Binaries

 

HTTPS 서버를 구축하기 위해서는 SSL 인증서가 필요한데, 로컬서버에 사용되는 SSL 인증서는 OpenSSL를 이용하여 쉽게 발급가능하다. 아래 링크에서 Windows용 OpenSSL을 설치한다.

 

https://slproweb.com/products/Win32OpenSSL.html

 

환경변수 path에 OpenSSL의 bin 폴더 경로를 추가한다.

인증서는 개인키와 공개키를 이용하는데, 아래 명령어로 개인키를 생성한다.

$ openssl genrsa 1024 > private.pem

생성된 개인키를 이용하여 공개키를 생성한다.

$ openssl req -x509 -new -key private.pem > public.pem

 

* greenlock 모듈을 이용하여 HTTPS 서버를 구축하는 방법도 있음.

https://www.zerocho.com/category/NodeJS/post/59f0efe01dc7c80019aca9f1

 

2. 로컬 서버 구축

https 모듈을 이용한 로컬 서버 실행 코드는 아래와 같다. 위에서 생성한 개인키(private.pem)와 공개키(public.pem) 파일을 서버 생성 메소드에 객체형식의 인수로 전달한다.

const https = require('https');
const express = require('express');
const fs = require('fs');

const app = express();
const port = 3000;

app.use('/', express.static('./public'));

const privateKey = fs.readFileSync('openssl/private.pem');
const certificate = fs.readFileSync('openssl/public.pem');
const options = {
    key: privateKey,
    cert: certificate
};

const httpsServer = https.createServer(options, app);
httpsServer.listen(port, function () {
    console.log("HTTPS server listening on port " + port);
});

 

'Node.js' 카테고리의 다른 글

[Node.js] Addons  (0) 2021.03.08
[Node.js] JavaScript Runtime Environment  (0) 2021.02.27
[Node.js] Worker Threads  (0) 2020.02.04
[Node.js] FormData 객체 전송  (0) 2020.01.29
[Node.js] TCP/IP 통신  (0) 2020.01.16
Comments