前言
之前看了很多人的文章教怎么將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ì)了吧?