前提条件

  • Ubuntu 16.04
  • Git/GitHub
  • Node.js
  • ThinkJS
  • 翻墙
  • 使用 root 用户

注册 keroku

https://www.heroku.com/

创建 ThinkJS 应用

thinkjs new thinkjsheroku
cd thinkjsheroku
npm install

注意: Do you want to turn on babel? (Y/n) n

修改默认端口为:80,打开配置文件

vim src/config/config.production.js

加入 port: process.env.PORT

更改 package.json 的 engins 为 "node": ">=8.0.0"

添加到版本管理

git init
git add .
git commit -m "first commit"

安装 Heroku 客户端

  • 安装
curl https://cli-assets.heroku.com/install-standalone.sh | sh

官方文档:https://devcenter.heroku.com/articles/heroku-cli

  • 验证安装
heroku --version

如果看到输出 heroku/x.y.z 则安装成功。

  • 登录 Heroku
heroku login

输入在 https://www.heroku.com/ 注册时的邮箱和密码。
也可以通过 SSH Keys 的方式登录,官方文档:https://devcenter.heroku.com/articles/keys

部署到 Heroku
创建 Heroku 应用

heroku create

创建完成会输出:

Creating app... done, ⬢ ancient-journey-68546
https://ancient-journey-68546.herokuapp.com/ | https://git.heroku.com/ancient-journey-68546.git

其中 ancient-journey-68546
为随机生成的应用名称,可在创建时指定一个唯一的名称。应用名称后面还有两个网址,其中第一个网址为网站的访问地址,最后一个是 Git 仓库地址。

配置 Heroku

  • 添加 Procfile
vim Procfile
echo "web: node production.js" > Procfile
  • 推送项目到 Heroku
git add .
git commit -m "add Procfile"
git push heroku master

只要有代码修改,重复上面的步骤即可更新到 Heroku 并重新部署

推送完成后可使用以下命令快速访问线上应用

heroku open

或直接有浏览器打开链接

标签: Node.js, ThinkJS