Skip to content

Sequelize 步驟 & heroku #22

@ddylanlinn

Description

@ddylanlinn

npm install
npm init
npm install bcrypt
npm install body-parser
npm installbcypt connect-flash
npm install ejs
npm install express
npm install express-session
npm install --save sequelize
npm install --save mysql2
npm install mysql
npm install --save sequelize-cli
安裝各檔案

npx sequelize-cli init
到config修改db登入資訊
規劃好資料結構 建立models
npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string

npx sequelize-cli db:migrate


heroku

1.在專案的package.json中的
script裡加入
"db:migrate": "npx sequelize db:migrate", // 執行sequelize migrate 產生資料庫
"start": "npm run db:migrate && node index.js", // 執行 index

另外加入引擎執行版本
"engines": {
"node": "14.x"
},

2.在index.js中的port加入環境變數
const port = process.env.PORT || 5001

3.建立版本控制 $git init

4.建立 .gitignore檔
填入 node_modules //不上傳

5.git add + commit

6.$heroku create

7.$git push heroku master

此時靜態部屬完成
若有資料庫則會出現錯誤
$heroku logs --tail 檢視錯誤訊息

8.點選ADD-ONS 的 ClearDB MySQL (or heroku Postgres)

9.照文件輸入
$heroku addons:create cleardb:ignite

10.回到 heroku 個人頁面新建的專案底下
設定裡面的 Config Vars 會有 CLEARDB_DATABASE_URL
及 mysql://帳號:密碼@host DATABASE?reconnect=true

11.回到本地 config資料夾中的 config.json 中
在production項目中新增 "use_env_variable": "CLEARDB_DATABASE_URL"

  1. git add + commit + push 即可

若要使用其他DB軟體連上資料庫
HOST 帳號 密碼為個人頁面該專案底下 CLEARDB_DATABASE_URL
mysql://帳號:密碼@host DATABASE?reconnect=true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions