February 8, 2021

https


https 모듈은 웹 서버에 SSL 암호화를 추가한다. 오가는 데이터를 암호화해서 중간에 다른 사람이 요청을 가로채더라도 내용을 확인할 수 없게 한다. https는 암호화를 적용하므로 그것을 인증해줄 수 있는 기관이 필요하다.

https 모듈 사용 예제

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

// 첫 번째 인수 : 인증서에 관련된 옵션 객체
// 두 번째 인수 : 서버 로직
https.createSever({
	cert: fs.readFileSync('도메인 인증서 경로'),
	key: fs.readFileSync('도메인 비밀키 경로'),
	ca: [
		fs.readFileSync('상위 인증서 경로'),
		fs.readFileSync('상위 인증서 경로'),
	],
}, (req, res) => {
	res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
	res.write('<h1>Hello Node!</h1>');
	res.end('<p>Hello Server!</p>');
}).listen(443);
// http와 달리 https의 기본 포트는 443이다.

http2


http2 모듈은 SSL 암호와와 더불어 http/2 프로토콜을 사용할 수 있게한다. http/2를 사용하면 요청 및 응답 방식이 기존 http/1.1보다 개선되어 효율적으로 요청을 보낸다.

http/2와 http/1.1

http/2는 한 커넥션에 여러개의 메시지를 주고받을 수 있다.

<aside> 💡 실제로는 http/1.1은 파이프라인 기술을 사용하기 때문에 큰 차이는 나지 않지만 http/2가 효율적이다.

</aside>

<aside> 💡 네트워크 하나도 기억 안난다.....

</aside>

http2 사용 예제

const http2 = require('http2');
const fs = require('fs');

// 첫 번째 인수 : 인증서에 관련된 옵션 객체
// 두 번째 인수 : 서버 로직
http2.createSecureSever({
	cert: fs.readFileSync('도메인 인증서 경로'),
	key: fs.readFileSync('도메인 비밀키 경로'),
	ca: [
		fs.readFileSync('상위 인증서 경로'),
		fs.readFileSync('상위 인증서 경로'),
	],
}, (req, res) => {
	res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
	res.write('<h1>Hello Node!</h1>');
	res.end('<p>Hello Server!</p>');
}).listen(443);
// http와 달리 https의 기본 포트는 443이다.

https→http2

createServer→createSecureServer