部署:Heroku


Posted by ericcch24 on 2021-05-16

提供一個可以省去一堆麻煩設定(例如設定防火牆、資料庫等等)的空間可以使用。

環境變數

  • environment variable: 設定在主機裡的變數
  • 在 Node.js 拿取環境變數:process.env.變數名稱
  • 常用於存取敏感資訊,這樣敏感程式碼就不會直接看到。

  • 使用範例 1
    test.js

    console.log(process.env.ABC)
    

    CLI:

    ABC=123 node test.js // 設定 ABC = 123
    
    output: 123 // process.env.ABC 拿 ABC 的值
    
  • 使用範例 2

CLI:
設定環境變數

export UNAME=5566

拿取值 兩者皆可

echo $UNAME 
node test.js
output: 5566

部署步驟

參考資料:部署 Node.js app 在 Heroku

git 設定補充:

  • 建立 git: git init
  • 建立 gitignore 檔,把node_modules/ 放進去
  • 把檔案加入 git: git add .
  • commit: git commit -am init
  • 在 heroku 建立 app: heroku create
  • 把 branch push 到 heroku:git push heroku master
  • heroku URL
    mysql://b5b216222d238d:23a766bc@us-cdbr-east-02.cleardb.com/heroku_09baaabca9d382c?reconnect=true
    // username: be6c96a16xxxd
    // password: c504fxxx
    // host: us-cdbr-iron-east-05.cleardb.net
    // database: heroku_e8d000339887xxx
    

註:heroku 會員密碼前面有「!」


同場加映:部署 json server 至 heroku 遇到的錯誤

部署步驟

錯誤解答參考資料

錯誤顯示:
heroku Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

解決方法:

const HOST = process.env.HOST || '0.0.0.0';
const PORT = process.env.PORT || 3310;

server.listen(PORT, HOST, () => {
  console.log('JSON Server is running: http://localhost:' + PORT);
});

PORT 與 HOST 都要加到 listen!!!


heroku 資料庫 crash

log: This database engine version is not supported, please update your database server.

heroku addons:upgrade 變成升級要錢的方案
然後去 heroku 調整 ClearDB 的 add-ons 調回免費的就好了 不知道為什麼。

tags: Week17

#week17







Related Posts

Pinia練習-製作購物車

Pinia練習-製作購物車

Show Me the DAG - An Introduction to Causal Graphs

Show Me the DAG - An Introduction to Causal Graphs

[html] 暗藏玄機的跨域連結 - link rel

[html] 暗藏玄機的跨域連結 - link rel


Comments