기록/node.js 교과서 따라하기
210915 [4장] 4.4 https와 http2
코드코코
2021. 9. 15. 23:46
1. server1.js
const http = require('http');
http.createServer((req, res) => {
res.writeHead(500, { 'content-Type': 'text/html; charset=utf-8' });
res.writeHead('<h1>Hello Node!</h1>');
res.end('<p>Hello Server!</p>');
})
.listen(8089, () => {
console.log('8089번 포트에서 서버 대기 중입니다.');
})
// 이 서버에 암호화를 적용하려면 https모듈을 사용해야 함.
// 인증기관의 인증이 필요함.
//https 모듈 : 웹서버에 SSL암호화를 추가.
//SSL이 적용된 웹사이트 방문시 브라우저 주소창에 자물쇠 표시가 등장.
2. server 1-3.js
// 발급받은 인증서 있는 경우 HTTPS
const https = require('https');
const fs = require('fs');
//createServer메서드가 인수를 두 개 받음.
https.createServer({
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, () => {
console.log('443번 포트에서 서버 대기 중입니다.')
})
3. server1-4.js
// http2 적용시 : https 모듈과 유사
// https -> http2 , createServer -> createSecureServer 바꾸면 됨.
const http2 = require('http2');
const fs = require('fs');
//createServer메서드가 인수를 두 개 받음.
http2.createSecureServer({
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, () => {
console.log('443번 포트에서 서버 대기 중입니다.')
})