在Heroku 上部署Node.js 應(yīng)用

前言

之前看了很多人的文章教怎么將node.js部署到heroku上去,但是出了很多錯。所以這里整理一下我部署的過程,然后寫一篇文章,供大家參考。

部署到Heorku

1,在GitHub上新建一個myblog的repo,除了Repository name 什么也不要填寫,直接點(diǎn)擊Create repository 創(chuàng)建成功。
2,本地(gitbash)創(chuàng)建myblog,touch一個index.html,里面隨便寫一句話。再touch一個index.js,里面寫上:

let server = http.createServer(function (req,res){
    console.log(req.url)
    res.statusCode = 201
    res.write('Hello');
    res.end()
})
var port = process.env.PORT
server.listen(port||8888, function(){
     console.log(`Server is listening on port 8888`)
})

在gitbash上運(yùn)行node index.js,然后在Google用localhost:8888打開就可以看到“hello”,說明運(yùn)行沒問題。
然后再touch一個package.json,再git init -y 就會多了一個package.json,把里面的scripts添加一句 "start": "node index",像這樣:

image.png

touch一個 .ignore 里面寫上
image.png

3, git push -u origin master,提交代碼。
4,git checkout -b heroku.(新建分支heroku,master 分支,)
5,touch一個procfile 文件,內(nèi)容為: web: npm run heroku
6,在Heroku 上注冊用戶。之后下載安裝Heroku 的命令行工具包Toolbelt,點(diǎn)擊下載
7,在命令行中輸入:heroku login 登錄heroku,(Email 和Password 是你注冊Heroku 時候的信息)
8,在命令行中輸入:heroku:create, 在Heroku 上創(chuàng)建應(yīng)用.
9,部署代碼到Heroku 上.在命令行中依次輸入:

git add .
git commit -am "make it better"
git push heroku master

10,部署成功之后,在命令行中輸入:heroku open 然后會跳出來一個你的主頁,上面有hello,說明成功。
11,別忘了將代碼提交到github 庫。

git status
git add .
git commit -a(編輯內(nèi)容“部署成功”)
git push origin master

后記

以上都是我親自試過的,步驟夠詳細(xì)了吧?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容