我們經(jīng)常使用前端的免費空間GitHub,但是后端經(jīng)常聽到的是Heroku云平臺,目前支持以下語言。本文主要介紹Node.js組合MySQL與Git上傳。
本篇目錄
環(huán)境建置
注冊
安裝Heroku Cli
登入Heroku云平臺
布署
Git Ignore
建立Git
新增/連接APP
布署Heroku云平臺
MySQL配備
Session Store
報錯處理
環(huán)境建置
注冊
首先,我們先進入官網(wǎng),選擇注冊(Sign up)
填寫姓名、郵件等信息
等候Heroku云平臺將驗證信寄到信箱后,點選連接并連接到輸入密碼
完成
安裝Heroku cli
以后我們都要用Heroku cli先附上下載頁面。連接后,根據(jù)您的系統(tǒng)安裝合適的版本。
另外,我們會組合Git上傳專案,Git安裝可參考此處,
關於Git版本控制可參考高見龍大Git教學。
image
為了保證以后的順利運行,我們還需要Node版本8以上npm,您可以使用以下指令查詢是否已安裝。
$node--version
v10.16.1
$npm--version
6.12.0
登入Heroku
在完成上述環(huán)境建設后,您可以在終端上輸入:
$herokulogin
heroku:Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/xxx
Logging in...done
Logged in as xxxxxxxxx@gmail.com
這時會打開Heroku登錄頁面,輸入帳號密碼OK了!
布署
資料夾目前如下:
Git Ignore
在Git上傳前應該做的重大事情。
專案內(nèi)有個node_modules不需要上傳資料,因為他容量大,一般來說Heroku云平臺會自動根據(jù)package.json安裝所需的套件。因此,我們需要先建立它.gitignore文件和新的忽略規(guī)則。
建立Git
再來建立專案Git版本控制:
//進入專用數(shù)據(jù)
$cd chatroom
$git init
$git add.
$git commit -m 'first commit'
新增/連接APP
新增HerokuAPP,后面沒有名字Heroku會自己給與APP隨機名字。
$ heroku create [app-name]
Creating ? thef2e-chatroom...done
https://thef2e-chatroom.herokuapp.com/|
https://git.heroku.com/thef2e-chatroom.git
請注意,如果您想更改項目名稱,請訪問官方網(wǎng)站setting更改他的兩個網(wǎng)站不會改變,所以輸入以下指令更安全!
$herokuapps:rename--app[old-name][new-name]
現(xiàn)在,我們得到了APP網(wǎng)址以及Git數(shù)據(jù)庫網(wǎng)站。我們使用它Heroku將本地專案與遠端聯(lián)系起來:
$herokugit:remote-a[app-name]
//查看
$ git push heroku master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 10.87 KiB | 5.44 MiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Node.js app detected
remote:
remote: -----> Creating runtime environment
remote:
remote: NPM_CONFIG_LOGLEVEL=error
remote: NODE_ENV=production
remote: NODE_MODULES_CACHE=true
remote: NODE_VERBOSE=false
remote:
remote: -----> Installing binaries
remote: engines.node (package.json): unspecified
remote: engines.npm (package.json): unspecified (use default)
remote:
remote: Resolving node version 12.x...
remote: Downloading and installing node 12.13.0...
remote: Using default npm version: 6.12.0
remote:
remote: -----> Installing dependencies
remote: Installing node modules (package.json + package-lock)
remote: added 123 packages from 102 contributors and audited 239 packages in 3.359s
remote: found 0 vulnerabilities
remote:
remote:
remote: -----> Build
remote:
remote: -----> Pruning devDependencies
remote: audited 239 packages in 1.408s
remote: found 0 vulnerabilities
remote:
remote:
remote: -----> Caching build
remote: - node_modules
remote:
remote: -----> Build succeeded!
remote: ! This app may not specify any way to start a node process
remote: https://devcenter.heroku.com/articles/nodejs-support#default-web-process-type
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> web
remote:
remote: -----> Compressing...
remote: Done: 23.1M
remote: -----> Launching...
remote: Released v3
remote: https://thef2e-chatroom.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/thef2e-chatroom.git
* [new branch] master -> master
如果你按照我的倉庫步驟建立一個項目,你會發(fā)現(xiàn)終端有一行顯示驚嘆號,因為Heroku需要一行Start指示建立專案,我們package.json在報錯之前,檔中沒有明確的指示。
所以我們需要在那里JSON檔內(nèi)多新增一行start,并重新push一次,Heroku環(huán)境建設成功。
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
MySQL配備
然后我們來到第二個價格,準備把當?shù)氐臄?shù)據(jù)庫push上來,這次使用的服務是ClearDB。
我們必須首先增加一個新的項目Add-ons:
$ heroku addons:create cleardb:ignite
Creating cleardb:ignite on ? thef2e-chatroom... free
Created cleardb-octagonal-43734 as CLEARDB_DATABASE_URL
Use heroku addons:docs cleardb to view documentation
還可以在Heroku新網(wǎng)站:
image
通過以下指令,我們可以知道ClearDB數(shù)據(jù)庫配備:
$ heroku config | grep CLEARDB_DATABASE_URL
CLEARDB_DATABASE_URL:mysql://be6c96a165xxx0:c504fxxx@us-cdbr-iron-east-05.cleardb.net/heroku_e8d000339887xxx?reconnect=true
// 補充
// username: be6c96a16xxxd
// password: c504fxxx
// host: us-cdbr-iron-east-05.cleardb.net
// database: heroku_e8d000339887xxx
接著開啟MySQLWorkbench,新的連接可以連接到新的數(shù)據(jù)庫,然后連接到當?shù)囟薲atabase導出,引入遠端數(shù)據(jù)庫OK了!
image
順便說一句,由于遠端數(shù)據(jù)庫的增加,我們原程序碼中連接的本地端數(shù)據(jù)庫也需要更換為遠端數(shù)據(jù)庫!
SessionStore
最后,假如你有介紹Session在項目中,還需要添加一個新的配備特性store,也就是Session存放的地方,在express-sessionnpm下面可以找到很多存儲方法。
image
我們選用express-mysql-session,同樣先安裝:
$ npm install express-mysql-session --save
并引入和使用(設置在Session上方):
const MySQLStore = require('express-mysql-session')(session);
const options = {
connectionLimit: 10,
host: 'us-cdbr-iron-east-05.cleardb.net',
user: 'be6c96a165xxx0',
password: 'c504fxxx',
database: 'heroku_e8d000339887xxx'
}
const sessionStore = new MySQLStore(options);
// 此處為 session 設置
app.use(session({
secret: 'thef2e_chatroom',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 60 * 60 * 1000 * 3,
},
}));
報錯處理
在APP網(wǎng)站右上角可以打開APP(如果你看到這個頁面代表錯誤。
此時可以使用以下指令查詢(使用以下指令)ctrlc撤出):
$ heroku logs --tail
如果你報錯了,多看終端,仔細看肯定會發(fā)現(xiàn)問題!
以上是不讀報錯的最大經(jīng)驗。
聲明本文內(nèi)容來自網(wǎng)絡,若涉及侵權,請聯(lián)系我們刪除! 投稿需知:請以word形式發(fā)送至郵箱18067275213@163.com
當局者迷,旁觀者清,存在競爭,才會進步