티스토리 뷰

2017/01/10 - [Computer/node.js] - node.js ) 8일차 - DB 이용해보기 - SELECT (MySQL)

2017/01/09 - [Computer/node.js] - node.js ) 7일차 - Express Framework ' Hello World '

2016/12/21 - [Computer/node.js] - node.js ) 6일차 - 의존성

2016/12/17 - [Computer/node.js] - node.js ) 5일차 - 모듈화

2016/12/17 - [Computer/node.js] - node.js ) 4일차 - 이벤트형 처리

2016/12/16 - [Computer/node.js] - node.js ) 3일차 - 함수 작성

2016/12/16 - [Computer/node.js] - node.js ) 2일차 - 웹서버 구동

2016/12/16 - [Computer/node.js] - node.js ) 1일차 - Hello World !



( 진행하기전에 저는 맥 환경에서 작업하였습니다. )




1. 기본 구조


우리가 지금까지 만들었던 서버 구조는 아래와 같다.




package.json 으로 node_module이 설치된 상태이며, 

server.js 에는 8일차까지 진행했던 스펙 그대로다.




2. ssl 인증서를 만들기 위해 개인키와 디지털 인증서를 만들기


우선, pem 파일 2개를 만들기 위해 맥의 터미널을 열어 우리가 만들었던 프로젝트로 들어가서 [fake-keys] 라는 폴더를 만들어보자.




이제 맥의 터미널을 열어 fake-keys로 들어간다.

아래의 명령어를 입력한다.


iyeong-uui-MacBook-Pro:fake-keys meongchong-ja$ openssl genrsa 1024 > key.pem

Generating RSA private key, 1024 bit long modulus

..........++++++

...++++++

e is 65537 (0x10001)


아래와 같이 개인키가 생성되었다.




이제, 개인키를 이용하여 디지털 인증서를 만들어 보자.
마찬가지로 같은 폴더에서(fake-keys) 아래의 명령어를 입력해 준다.

iyeong-uui-MacBook-Pro:fake-keys meongchong-ja$ openssl req -x509 -new -key key.pem > cert.pem

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [AU]:KO

State or Province Name (full name) [Some-State]:Seoul

Locality Name (eg, city) []:Seoul

Organization Name (eg, company) [Internet Widgits Pty Ltd]:LEE

Organizational Unit Name (eg, section) []:LEE

Common Name (e.g. server FQDN or YOUR name) []:LEE

Email Address []:marlboroyw@naver.com


만드는 과정에서 몇가지 정보를 입력해 줘야 하는데 어차피 우리는 공인된 인증서가 아니다. (공식기관으로부터 인증받은 공인된 인증서를 발급받으려면 돈이 필요하다. (2년에 얼마 요런식..))


이제 우리는 두 키를 발급받았다. 


3. server.js 작성


이제, 생성된 두 키를 가지고 server.js 를 아래와 같이 작성해보자.


var http = require('http');

var https = require('https');

var express = require('express');

var fs = require('fs');

var app = express();

var router = require('./router/main')(app);

var options = {

    key: fs.readFileSync('fake-keys/key.pem'),

    cert: fs.readFileSync('fake-keys/cert.pem')

};



var portForHttp = 8000;

var portForHttps = 8001;


app.set('views', __dirname + '/view');

app.set('view engine', 'ejs');

app.engine('html', require('ejs').renderFile);


http.createServer(app).listen(portForHttp, function() {

  console.log("Http server listening on port " + portForHttp);

});


https.createServer(options, app).listen(portForHttps, function() {

  console.log("Https server listening on port " + portForHttps);

});


보통, http 서버는 80 포트, https 포트는 443 포트를 이용하지만..

우리의 맥북은 로컬에서 80 과 443을 이미 사용하고 있기 때문에, 

임의로 8000 과 80001번을 사용해보자.



4. 실행

iyeong-uui-MacBook-Pro:gchat meongchong-ja$ node server.js 

Http server listening on port 8000

Https server listening on port 8001


8000과 8001 번 포트가 실행중이다. ㅇ

우리의 브라우저로 들어가보자.

http 일반포트는 문제없이 들어가진다.



8001번 https 로 들어가보자.



위와 같이 나올것이다. 당황 ㄴㄴ

공인된 기관에서 받은 인증서가 아니라 우리가 연습용으로 만든것이라 그렇다.


고급 버튼을 눌러 페이지로 들어가자


성공






공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함