美文网首页
在Heroku 上部署Node.js 应用

在Heroku 上部署Node.js 应用

作者: 成成成汤 | 来源:发表于2019-08-08 17:10 被阅读0次

前言

之前看了很多人的文章教怎么将node.js部署到heroku上去,但是出了很多错。所以这里整理一下我部署的过程,然后写一篇文章,供大家参考。

部署到Heorku

1,在GitHub上新建一个myblog的repo,除了Repository name 什么也不要填写,直接点击Create repository 创建成功。
2,本地(gitbash)创建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上运行node index.js,然后在Google用localhost:8888打开就可以看到“hello”,说明运行没问题。
然后再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 文件,内容为: web: npm run heroku
6,在Heroku 上注册用户。之后下载安装Heroku 的命令行工具包Toolbelt,点击下载
7,在命令行中输入:heroku login 登录heroku,(Email 和Password 是你注册Heroku 时候的信息)
8,在命令行中输入:heroku:create, 在Heroku 上创建应用.
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(编辑内容“部署成功”)
git push origin master

后记

以上都是我亲自试过的,步骤够详细了吧?

相关文章

网友评论

      本文标题:在Heroku 上部署Node.js 应用

      本文链接:https://www.haomeiwen.com/subject/zzixjctx.html