JobbyM's Blog

在Heroku 上部署Node.js 应用

前言

工作之余,按照nswbmw一起学Node.js教程,一步一步实现了一个Express + MongoDB 的多人博客,并部署到了Heroku 上。

本篇教程就是在4.15 部署的基础上,按照自己的实际操作记录的部署过程。

部署到Heorku

  1. 首先在github.com 上新建一个my-blog 的repo, 注意:除了Repository name 什么也不要填写 点击Create repository 创建成功
  2. 本地进入D:\nodejs\my-blog 添加远端仓库
    1
    D:\nodejs\my-blog>git remote add origin https://github.com/JobbyM/my-blog.git
  3. 提交代码
    1
    D:\nodejs\my-blog>git push -u origin master
  4. 新建分支heroku,master 分支为一起学Node.js 教程的4.15 部署 之前的内容
    1
    D:\nodejs\my-blog>git checkout -b heroku
  5. 删除掉.gitignore 中的
    1
    2
    config/*
    !config/default.*
    因为我们无法登录到Heroku 主机创建production 配置文件,所以这里将productin 配置也上传到Heroku。
  6. 打开index.js,将app.listen 修改为
    1
    2
    3
    4
    var port = process.env.PORT || config.port
    app.listen(port, function(){
    console.log(`${pkg.name} listening on port ${port}`)
    })
  7. 新增Heroku 需要的Procfile 文件,内容为
    1
    web: npm run heroku
    同时修改package.json 文件,在scripts 中新增如下命令
    1
    "heroku": "NODE_ENV=production node index"
  8. Heroku 上注册用户,之后下载安装Heroku 的命令行工具包Toolbelt。参考这里下载安装
  9. 在命令行中登录Heroku,Email 和Password 为注册Heroku 的相关信息
    1
    2
    3
    4
    5
    D:\nodejs\my-blog>heroku login
    Enter your Heroku credentials:
    Email: songzhipengmune@gmail.com
    Password: *********
    Logged in as songzhipengmune@gmail.com
  10. 在Heroku 上创建应用
    1
    D:\nodejs\my-blog>heroku create
  11. 部署代码到Heroku 上
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    D:\nodejs\my-blog>git push heroku heroku:master
    Counting objects: 209, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (194/194), done.
    Writing objects: 100% (209/209), 372.78 KiB | 0 bytes/s, done.
    Total 209 (delta 104), 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: NPM_CONFIG_PRODUCTION=true
    remote: NODE_VERBOSE=false
    remote: NODE_ENV=production
    remote: NODE_MODULES_CACHE=true
    remote:
    remote: -----> Installing binaries
    remote: engines.node (package.json): unspecified
    remote: engines.npm (package.json): unspecified (use default)
    remote:
    remote: Resolving node version 6.x via semver.io...
    remote: Downloading and installing node 6.10.3...
    remote: Using default npm version: 3.10.10
    remote:
    remote: -----> Restoring cache
    remote: Skipping cache restore (new-signature)
    remote:
    remote: -----> Building dependencies
    remote: Installing node modules (package.json)
    remote: my-blog@1.0.0 /tmp/build_68cbc0c86c867e77d30b4dabaed7f230
    remote: ├─┬ config-lite@2.0.0
    remote: │ ├─┬ chalk@1.1.3
    remote: │ │ ├── ansi-styles@2.2.1

    remote:
    remote:
    remote: -----> Caching build
    remote: Clearing previous node cache
    remote: Saving 2 cacheDirectories (default):
    remote: - node_modules
    remote: - bower_components (nothing to cache)
    remote:
    remote: -----> Build succeeded!
    remote: -----> Discovering process types
    remote: Procfile declares types -> web
    remote:
    remote: -----> Compressing...
    remote: Done: 17.7M
    remote: -----> Launching...
    remote: Released v3
    remote: https://xx.herokuapp.com/ deployed to Heroku
    remote:
    remote: Verifying deploy... done.
    To https://git.heroku.com/xx.git
    * [new branch] heroku -> master
    注意:必须是这样部署,否则将会跳过构建,例如如下部署
    1
    D:\nodejs\my-blog>git push heroku heroku
    提示
    1
    2
    3
    4
    5
    6
    7
    8
    9
    D:\nodejs\my-blog>git push heroku heroku
    Counting objects: 209, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (194/194), done.
    Writing objects: 100% (209/209), 372.78 KiB | 0 bytes/s, done.
    Total 209 (delta 104), reused 0 (delta 0)
    remote: Pushed to non-master branch, skipping build.
    To https://git.heroku.com/murmuring-fjord-81424.git
    * [new branch] heroku -> heroku
  12. 部署成功之后,使用
    1
    D:\nodejs\my-blog>heroku open
    打开应用主页
  13. 将代码提交到github 库
    1
    D:\nodejs\my-blog>git push origin heroku:heroku