54ziziyo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [學習筆記] .env 環境變數 ## .env 環境變數是什麼? :::info :bulb: Env 全名是 Environment 中文又稱環境變數,不論是前端或者後端開發上都是非常常見的東西。 ::: Env 主要是拿來做什麼呢?通常是拿來存放一些比較敏感的資訊,例如常見的… * key(金鑰) * secret(密碼) * token(權杖) * …等等 因為我們的程式碼裡面絕對不能出現任何敏感資訊,因此我們會把這些敏感資訊放在 Env 裡面,然後在程式碼中使用 Env 來取得這些敏感資訊。 通常 Env 的檔案會叫做 .env,而你有可能會看到以下類型的 .env 檔案。 * .env * .env.development * .env.production * .env.test <br> ## 舉例來說 這些都是不同的 Env 檔案,可以在不同的環境下取用不同的 Env,例如在開發環境下使用 .env.development,而在正式環境下使用 .env.production。 重點在於不同的 .env 可以自由切換,如果要換環境,那就要一直改code會很麻煩。 <br> 以 Vite來舉例的話,.env 是vite提供的環境配置。 ###### => 執行 npm run dev 的時候,vite會吃「env.development」 ###### => 執行 npm run build 的時候,vite會吃「env.production」或「.env」 ###### => 執行 npm test 的時候,vite會吃「env.testing」 <br> 伺服器上面以點開頭的檔案都是隱藏檔案,不會暴露給外部,它不會參與打包。 ![](https://hackmd.io/_uploads/SyRUcXm62.jpg) <br> 通常來講我們不會把 .env 加入到 Git 版本控制內,如前面所說 .env 檔案主要是放置敏感資訊,因此 .env 通常會被加入到 .gitignore,這樣子才不會不小心把敏感資訊上傳到遠端儲存庫上。 如果不小心傳上去,趕快上去刪掉你的遠端儲存庫,並把 .env 內的資訊更換掉 ([文章來源](https://israynotarray.com/other/20230218/3618693250/)) <br> ## 應用 先創建一個.env⬇︎ ```javascript! // .env VITE_APP_NAME=example VITE_API_URL=https://api.example.com ``` <br> 1. 可以這樣用⬇︎ ```javascript! // @/src/http/config.js /** * @description: http请求配置 */ export default { baseURL: import.meta.env.VITE_API_URL, timeout: 20000, headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', }, } ``` <br> 2. 可以這樣用⬇︎ ```javascript! // App.vue <template> <div> 這是一句話 </div> </template> <script setup> fetch(import.meta.env.VITE_API_URL) .then(response => response.json()) .then(json => console.log(json)) </script> ``` <br> 我們可以這樣做,在 javascript 去讀取這個環境變數。⬇︎ ![](https://hackmd.io/_uploads/Bk_wG77p3.png) <br> :::warning 因此其實前端的 Env 並不適合放一些敏感資訊,比較適合作為一些常用的設定管理。 例如:API Url、網站標題等,比較重複性高,未來若要調整可以一次調整的設定。 而這也是為什麼我在前面說「後端的 Env 非常的安全」,因為後端的程式碼是運行在伺服器上,而前端的程式碼是直接在瀏覽器上執行的,所以使用者只要將你的程式碼下載下來,就可以直接看到你的 Env 內容。 ::: <br> ## しかし (但是呢! 這樣的話,難道變數一定都會被看光光嗎? 不是嗲,請繼續看 ☟ <br> 加載的環境變數也會通過 `import.meta.env` 以字符串形式暴露給客戶端源碼。 為了防止意外將一些環境變數洩漏到客戶端,假設目前使用是 Vite 的話,以 `VITE_`為**前綴變量**,是經過 vite 處理的代碼(被vite解析後注入 javascript 中),只有加上這個前綴才會暴露給客戶端。 ```javascript! // .env VITE_SOME_KEY=123 DC_PASSWORD=foobar ``` * 只有 `VITE_SOME_KEY` 會暴露為 `import.meta.env.VITE_SOME_KEY` 提供給客戶端碼。 * `VITE_SOME_KEY` 則不會,反而會當成機密變數。 <br> :::danger **環境加載優先級** 指定模式的文件(ex: .env.production)會比通用形式的優先級更高(ex: .env)。 Vite 執行時,已經存在的環境變數有最高優先度,不會被 `.env` 類文件覆蓋。 (ex: 當運行到`VITE_SOME_KEY=123 vite build` 的時候) .env 類文件會在 Vite 啟動一開始時自動被加載,而改動的話會再重啟服務器後生效。 ::: <br> ## 那麼機密變數要怎麼用呢? 因為在 javascript 上用,會被看光光,且如果是靜態部署,基本上就不會用到機密變數。 前端開發也可能會涉及到金鑰存取,比如說密鑰路徑,一些秘密參數、串接金流、第三方api、身份認證、圖片影片上傳加解密。 如果你是用像 Vite 這樣有支援 SSR 的框架工具,就可以在服務器端設置環境變量,把機密變數運用在這個服務器上。 > 剛剛問了 ChatGPT,他是說可以 `const { createServerRenderer } = require('vite')` 或是 裝 `vite-ssr` 外掛,目前還沒試過,所以不知道XD <br> ## 有其他類似的框架也提供.env環境配置能嗎? 除了Vite之外的許多,前端框架和工具也提供了類似的一些環境配置功能,方便在不同的開發環境中配置不同的參數和選項。以下是其他前端框架和工具提供的環境配置選項: 1. Create React App (CRA) : CRA是一個用於快速創建React應用程序的腳手架工具,它使用.env文件來配置環境變量。您可以創建.env、.env.local、.env.development、.env.test等.env.production文件來分別不同的配置環境變量。 3. Vue CLI:Vue CLI是Vue.js官方提供的腳手架工具,類似Vite,它也支持.env文件來配置環境變量。您可以創建.env、.env.local、.env.development、.env.production等文件來配置不同環境的變量。 5. Webpack:Webpack是一個強大的資源工具,它也支持使用.env文件來配置環境變量。您可以使用.env、.env.development、.env.production等文件來配置不同的環境變量。 3. Next.js : Next.js是一個React框架,它提供了.env文件來配置環境變量。您可以創建.env、.env.local、.env.development、.env.production等文件來配置不同環境的變量。 5. 這些工具在項目中提供了一種靈活的方式來配置不同環境的變量,方便在開發、測試和生產環境中管理應用程序的配置。根據您使用的工具和框架,您可以選擇適合您項目的環境配置方式。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully