[Heroku + Git] Node.js项目配置

Heroku Setup

参考:

  1. 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
  2. Heroku Dev Center (2020). Deploying with Git. Retrieved from https://devcenter.heroku.com/articles/git.

专门针对 Node.js 的项目

Step 1 File setup

  1. 在项目文件夹下创建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"
     }
    
  2. 创建Procfile文件,文件名就叫Procfile,不写文件类型。
    文件内容:

web: node server.js
  1. 创建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

  1. 打开cmd,进入存储项目的文件夹。输入 npm install.
  2. 自己登录Heroku官网注册:www.heroku.com
  3. 选编程语言的时候选择node.js
  4. 自行安装Heroku Command Line: https://devcenter.heroku.com/categories/command-line
  5. 在cmd中用 heroku --version 确认heroku有没有安装成功。如果有版本信息了那就是正确安装了
  6. 登录命令: heroku login
  7. 启动本地服务:heroku local web 。本机地址默认为: localhost:5050.
  8. 停止本地服务: 在cmd中按Ctrl+C

Step 3. Add Git

可以用 git --version 确认一下已经安装了git。
已经在github中有项目了的话,直接跳到第四步。

  1. 初始化git库 git init
  2. git add
  3. git commit -m
  4. heroku create. 这个时候回到自己的heroku网页上能看到自己的账户里多了一个project。
  5. 把刚刚cmd自己跳出来的一坨信息里面的 https://git.heroku.com/blablabla.git找出来,复制。
  6. 输入 git push https://git.heroku.com/blablabla.git main. 有些人可能git里面的branch是master,那就把main改成master。
  7. cmd中会跳出来一大段,在最后倒数几行里有一个https://blablabla.herokuapp.com/的链接,把这个链接复制下来,直接去浏览器打开即可看到自己的网页。

Step 4. Update

在项目文件夹中打开cmd,输入 git push heroku main.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值