heroku线上部署node小程序

环境安装

首先我们需要个heroku账户,可以点这里申请,然后我们需要在本地下载安装它的工具包(我这里只讲Ubuntu,其他请转https://devcenter.heroku.com/…

wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

如果上面代码安装失败(可能只是我的电脑问题),并报认证之类的错误,那就把https://toolbelt.heroku.com/install-ubuntu.sh的代码复制到本地保存为install.sh,将第13行的http改为https,如下所示:

#!/bin/sh
{
    echo "This script requires superuser access to install apt packages."
    echo "You will be prompted for your password by sudo."

    # clear any previous sudo permission
    sudo -k

    # run inside sudo
    sudo sh <<SCRIPT

  # add heroku repository to apt
  echo "deb https://toolbelt.heroku.com/ubuntu ./" > /etc/apt/sources.list.d/heroku.list

  # install heroku's release key for package verification
  wget -O- https://toolbelt.heroku.com/apt/release.key | apt-key add -

  # update your sources
  apt-get update

  # install the toolbelt
  apt-get install -y heroku-toolbelt

  # install ruby if needed (vervet)
  if ! type ruby >/dev/null 2>&1; then
    apt-get install -y ruby
  fi

SCRIPT
}

然后再执行sh install.sh应该就没问题了
看是否安装成功,输入heroku --version,如果如下图之类输出的话表示安装成功
《heroku线上部署node小程序》
登录:执行命令heroku login,输入你的注册邮箱和密码,认证成功登录

代码部署

我以慕课网下载解析为例,首先从我的gitub上克隆这个项目到本地

git clone https://github.com/leoyaojy/imooc-video-downloader.git
cd imooc-video-downloader
heroku create    //创建一个app应用

修改package.json文件两处地方,添加对bower的支持,如下:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "postinstall": "./node_modules/bower/bin/bower install"  //增加此行
},
"dependencies": {
    "bower":"^1.7.9",    //增加此行
    "body-parser": "^1.15.2",
    "cheerio": "^0.22.0",
    "express": "^4.14.0",
    "jade": "^1.11.0",
    "request": "^2.74.0"
},

新建Procfile文件,里面内容为web: node app
接着执行以下几行代码,部署就完成了

git add .
git commit -m "deploy"
git push heroku master

最后执行heroku open就可以直接在浏览器打开,测试地址:https://pure-thicket-46482.he…

    原文作者:luckyw
    原文地址: https://segmentfault.com/a/1190000007542582
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞