Heroku Setup
参考:
- Hodge, K. (2017). Heroku Hosting: Put Your Website On the Cloud. Retrieved from https://medium.com/@blondiebytes/heroku-hosting-put-your-website-on-the-cloud-cdc8729a7e09
- Heroku Dev Center (2020). Deploying with Git. Retrieved from https://devcenter.heroku.com/articles/git.
专门针对 Node.js 的项目
Step 1 File setup
-
在项目文件夹下创建
package.json
{ "name": "xxx", // Name of the project "version": "1.0.0", "scripts": { "start": "node server.js" }, "dependencies": { "express": "4.13.3" // Nodejs framework }, "author": "Crystalalala", "license": "ISC" }
-
创建
Procfile
文件,文件名就叫Procfile,不写文件类型。
文件内容:
web: node server.js
- 创建
server.js
文件。文件内容:
var express = require('express');
var app = express();
// Set port: Heroku can set the port or be at 8080
var port = process.env.PORT || 8080;
app.use(express.static(__dirname))
// routes
app.get("/", function(req, res) {
res.render("index");
});
app.listen(port, function(){
console.log("app running");
});
注意:保证index.html和这些配置文件在同一目录中
Step 2. Environment Configuration
- 打开
cmd
,进入存储项目的文件夹。输入npm install
. - 自己登录Heroku官网注册:www.heroku.com
- 选编程语言的时候选择node.js
- 自行安装Heroku Command Line: https://devcenter.heroku.com/categories/command-line
- 在cmd中用
heroku --version
确认heroku有没有安装成功。如果有版本信息了那就是正确安装了 - 登录命令:
heroku login
- 启动本地服务:
heroku local web
。本机地址默认为:localhost:5050
. - 停止本地服务: 在cmd中按Ctrl+C
Step 3. Add Git
可以用 git --version
确认一下已经安装了git。
已经在github中有项目了的话,直接跳到第四步。
- 初始化git库
git init
git add
git commit -m
heroku create
. 这个时候回到自己的heroku网页上能看到自己的账户里多了一个project。- 把刚刚cmd自己跳出来的一坨信息里面的
https://git.heroku.com/blablabla.git
找出来,复制。 - 输入
git push https://git.heroku.com/blablabla.git main
. 有些人可能git里面的branch是master,那就把main改成master。 - cmd中会跳出来一大段,在最后倒数几行里有一个
https://blablabla.herokuapp.com/
的链接,把这个链接复制下来,直接去浏览器打开即可看到自己的网页。
Step 4. Update
在项目文件夹中打开cmd,输入 git push heroku main
.