1 创建GitHub帐号
2 创建Heroku帐号
- 现在Heroku网站上创建账号,
- 下载heroku cli 安装包,安装heroku cli, 安装完成执行命令 heroku -v 查看版本,
- 然后
heroku login
登录,
Issue: heroku login 通常会fail,不连vpn,Heroku连不上,连vpn,则显示ip mismatch。分两步,先断开vpn,browser点登录,会有一个等待过程,此时,连上vpn, login成功。
3 本地工程添加git版本控制
git init
- 添加文件
.gitignore
, 添加行node_modules/
git add .
将工程文件夹下所有untracked 文件状态设为staged.git commit -m "Init commit"
4 创建SSH
SSH: Secure Shell 或 Secure Socket Shell, 一种网络协议,用于为用户,尤其是系统管理员提供安全网络服务,实现不安全网络上对某台计算机的安全访问。
windows创建SSH步骤如下:
- 使用 git bash 执行命令:
ls -a -l ~/.ssh
其中 a: all,包括隐藏文件, l: 以列表显示,此命令用于查看电脑上是否有已经生成的SSH,如果没有类似于id_rsa, id_rsa.pub的文件,则执行下面的步骤生成SSH。 ssh-keygen -t rsa -b 4096 -C "mymail@163.com for SSH key"
, 生成私钥和公钥对,其中 t: type,使用 rsa, b: bit, C: comment , 显示 id_rsa 和 id_rsa.pub, id_rsa 仅存放在本机,不允许分享。eval $(ssh-agent -s)
检查ssh agent 是否正在运行。ssh-add ~/.ssh/id_rsa
显示 identity add 等cat ~/id_rsa.pub
, 将字符串copy 到GitHub上setting的SSH里。
5 本地代码push到GitHub
GitHub 是非常流行的软件开发平台,提供了用于管理软件开发项目所有必须的工具。
-
在GitHub 上创建新的repository,
然后, -
参考网页上的指令:
-
…or push an existing repository from the command line
git remote add origin https://github.com/my_github_account/mytest1.git
git branch -M main
git push -u origin main
这里需要注意的地方是,master 改成了main.
6 创建Heroku工程
- package.json 文件修改并测试:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start":"node src/app.js" // 增加这一行
},
然后用npm run start
命令进行本地测试
2. 端口号在Heroku上是动态生成的,一定要修改,不能写死成3000:
const port = process.env.PORT || 3000;
app.listen(port, () => console.log("Server listen at port " + port));
- 删除code里所有的
localhost:3000
字样,进行相应修改。 git commit -m "Setup for Heroku"
heroku keys:add
为Heroku添加SSHheroku create weather-20211111
create 后名称省略,则随机生成网址,否则需要自己设置不重复的URL。git push heroku main
代码部署到Heroku 上,此过程将自动安装npm包。