๐Ÿ” mkcert

์ง€์€ยท2022๋…„ 11์›” 13์ผ
0

Node.js Library

๋ชฉ๋ก ๋ณด๊ธฐ
13/14

mkcert

: ๋กœ์ปฌ ํ™˜๊ฒฝ(๋‚ด ์ปดํ“จํ„ฐ)์—์„œ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ฆ์„œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ
mkcert

mkcert ์„ค์น˜

Homebrew๋ฅผ ํ†ตํ•ด mkcert๋ฅผ ๋‹ค์šด ๋ฐ›๋Š”๋‹ค.

brew install mkcert

์ธ์ฆ์„œ ์ƒ์„ฑํ•˜๊ธฐ

1. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ๋กœ์ปฌ์„ ์ธ์ฆ๋œ ๋ฐœ๊ธ‰๊ธฐ๊ด€์œผ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

mkcert -install

2. ๋กœ์ปฌ ํ™˜๊ฒฝ์— ๋Œ€ํ•œ ์ธ์ฆ์„œ๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 ::1

โžก๏ธ ์˜ต์…˜์œผ๋กœ ์ถ”๊ฐ€ํ•œ localhost, 127.0.0.1(IPv4), ::1(IPv6)์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ฆ์„œ๊ฐ€ ๋ฐœ๊ธ‰๋œ๋‹ค.

3. cert.pem ํŒŒ์ผ๊ณผ key.pem ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

  • cert.perm : ๊ณต๊ฐœ ํ‚ค์™€ ์ธ์ฆ๊ธฐ๊ด€์˜ ์„œ๋ช…์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์ธ์ฆ์„œ(certificate) / ๊ณต๊ฐœ๋˜์–ด๋„ O
  • key.perm : ๊ฐœ์ธ ํ‚ค / ๊ณต๊ฐœ๋˜๋ฉด X

์ด๋•Œ ๋ฐœ๊ธ‰ ๋ฐ›์€ key์™€ cert์˜ ์ €์žฅ ๊ฒฝ๋กœ๋ฅผ ์•Œ๊ณ  ์žˆ์–ด์•ผ, ๋‚˜์ค‘์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


HTTPS ์„œ๋ฒ„ ์ž‘์„ฑํ•˜๊ธฐ

Node.js ํ™˜๊ฒฝ์—์„œ HTTPS ์„œ๋ฒ„๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด https ๋‚ด์žฅ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•ด ๋˜๊ณ , express.js๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

1. HTTPS ๋ชจ๋“ˆ ์‚ฌ์šฉ

const https = require('https'); // HTTPS ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
const fs = require('fs'); // File System ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

const option = {
  key: fs.readFileSync(__dirname + '/key.pem', 'utf-8'),
  cert: fs.readFileSync(__dirname + '/cert.pem', 'utf-8'),
}; // key.pem๊ณผ cert.pem ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๊ธฐ

https
  .createServer(option, (req, res) => {
    res.write('Congrats! You made https server now :)');
    res.end();
  })
  .listen(3000);
  • __dirname์€ ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ํด๋” ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    (key: fs.readFileSync('./key.pem', 'utf-8')์™€ ๊ฐ™์€ ์˜๋ฏธ)


2. express.js ์‚ฌ์šฉ

https.createServer()์˜ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋“ค์–ด๊ฐˆ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ express ๋ฏธ๋“ค์›จ์–ด๋กœ ๊ต์ฒดํ•˜๋ฉด ๋œ๋‹ค.

const https = require('https');
const fs = require('fs');
const express = require('express'); // express ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

const app = express(); // ์„œ๋ฒ„ ์‹คํ–‰

const option = {
  key: fs.readFileSync(__dirname + '/key.pem', 'utf-8'),
  cert: fs.readFileSync(__dirname + '/cert.pem', 'utf-8'),
};

https
  .createServer(
    option,
    app.use('/', (req, res) => {
      res.send('Congrats! You made https server now :)');
    })
  )
  .listen(3000);
profile
๋ธ”๋กœ๊ทธ ์ด์ „ -> https://janechun.tistory.com

0๊ฐœ์˜ ๋Œ“๊ธ€