建立 localhost 憑證 (Vue dev server)


1. 使用 mkcert 建立憑證

$ brew install mkcert
$ mkcert -install
Created a new local CA
$ mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1
Using the local CA
Created a new certificate valid for the following names 📜
 - "example.com"
 - "*.example.com"
 - "example.test"
 - "localhost"
 - "127.0.0.1"
 - "::1"
The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅

2. 將憑證放入專案資料夾,並在 vue.config.js 加入:

const fs = require('fs')
module.exports = {
    devServer: {
        https: {
          key: fs.readFileSync('./certs/example.com+5-key.pem'),
          cert: fs.readFileSync('./certs/example.com+5.pem'),
        },
        public: 'https://localhost:8080/'
    }
}

參考資料:
1. How to run Vue.js dev serve with https?
2. mkcert


發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *