第一个应用部署 #
准备工作 #
确认环境 #
bash
# 确认已安装 Heroku CLI
heroku --version
# 确认已登录
heroku whoami
# 确认已安装 Git
git --version
创建示例应用 #
项目结构 #
我们将创建一个简单的 Node.js 应用:
text
myapp/
├── index.js # 应用入口
├── package.json # 项目配置
├── Procfile # Heroku 进程配置
└── .gitignore # Git 忽略文件
创建项目目录 #
bash
# 创建项目目录
mkdir myapp
cd myapp
# 初始化 Git
git init
创建 package.json #
json
{
"name": "myapp",
"version": "1.0.0",
"description": "My first Heroku app",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"engines": {
"node": "20.x"
},
"dependencies": {
"express": "^4.18.2"
}
}
创建应用代码 #
javascript
// index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send(`
<html>
<head>
<title>My First Heroku App</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
text-align: center;
}
h1 { color: #7952b3; }
.info {
background: #f5f5f5;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
}
</style>
</head>
<body>
<h1>🚀 Hello Heroku!</h1>
<div class="info">
<p>恭喜!你已成功部署第一个 Heroku 应用。</p>
<p>当前时间: ${new Date().toLocaleString('zh-CN')}</p>
</div>
</body>
</html>
`);
});
app.get('/api', (req, res) => {
res.json({
message: 'Hello from Heroku!',
timestamp: new Date().toISOString()
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
创建 Procfile #
text
web: node index.js
Procfile 说明:
| 进程类型 | 命令 | 说明 |
|---|---|---|
| web | node index.js | Web 进程,接收 HTTP 流量 |
| worker | node worker.js | 后台任务进程(可选) |
| clock | node clock.js | 定时任务进程(可选) |
创建 .gitignore #
text
node_modules/
.env
.DS_Store
*.log
npm-debug.log*
本地测试 #
安装依赖 #
bash
# 安装依赖
npm install
本地运行 #
bash
# 启动应用
npm start
# 或使用 Heroku CLI 本地运行
heroku local web
访问 http://localhost:3000 查看应用。
测试 API #
bash
# 测试首页
curl http://localhost:3000
# 测试 API
curl http://localhost:3000/api
部署到 Heroku #
创建 Heroku 应用 #
bash
# 在当前目录创建应用
heroku create
# 指定应用名称
heroku create my-awesome-app-123
# 指定区域
heroku create --region eu
输出示例:
text
Creating my-awesome-app-123... done
https://my-awesome-app-123.herokuapp.com/ | https://git.heroku.com/my-awesome-app-123.git
查看应用信息 #
bash
# 查看应用信息
heroku apps:info
# 查看应用列表
heroku apps
# 在浏览器中打开应用
heroku open
Git 部署 #
bash
# 添加文件到 Git
git add .
git commit -m "Initial commit"
# 推送到 Heroku
git push heroku main
# 如果使用 master 分支
git push heroku master
部署过程:
text
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (10/10), 2.50 KiB | 2.50 MiB/s, done.
Total 10 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: -----> Node.js app detected
remote: -----> Creating runtime environment
remote: NPM_CONFIG_LOGLEVEL=error
remote: NODE_VERBOSE=false
remote: NODE_ENV=production
remote: NODE_MODULES_CACHE=true
remote: -----> Installing binaries
remote: engines.node (package.json): 20.x
remote: engines.npm (package.json): unspecified (use default)
remote: Resolving node version 20.x...
remote: Downloading and installing node 20.11.0...
remote: Using default npm version: 10.2.4
remote: -----> Installing dependencies
remote: Installing node modules
remote: added 58 packages in 2s
remote: -----> Pruning devDependencies
remote: removed 1 package in 0.1s
remote: -----> Build succeeded!
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote: -----> Compressing...
remote: Done: 20.5M
remote: -----> Launching...
remote: Released v1
remote: https://my-awesome-app-123.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/my-awesome-app-123.git
* [new branch] main -> main
确保应用运行 #
bash
# 确保至少有一个 Web Dyno 在运行
heroku ps:scale web=1
# 查看 Dyno 状态
heroku ps
访问应用 #
打开应用 #
bash
# 在浏览器中打开
heroku open
# 或直接访问 URL
# https://your-app-name.herokuapp.com
查看日志 #
bash
# 查看实时日志
heroku logs --tail
# 查看最近日志
heroku logs -n 100
# 查看特定 Dyno 的日志
heroku logs --dyno web.1
常用命令 #
应用管理 #
bash
# 列出所有应用
heroku apps
# 重命名应用
heroku apps:rename new-name
# 删除应用
heroku apps:destroy --app your-app-name
# 查看应用信息
heroku apps:info
配置管理 #
bash
# 设置环境变量
heroku config:set NODE_ENV=production
# 查看所有环境变量
heroku config
# 删除环境变量
heroku config:unset VARIABLE_NAME
进程管理 #
bash
# 查看 Dyno 状态
heroku ps
# 扩展 Dyno 数量
heroku ps:scale web=2
# 重启 Dyno
heroku restart
# 重启特定 Dyno
heroku restart web.1
运行命令 #
bash
# 运行一次性命令
heroku run node -v
# 进入交互式 Shell
heroku run bash
# 运行 Node REPL
heroku run node
部署流程图 #
text
┌──────────────┐
│ 本地代码 │
│ index.js │
│ package.json│
│ Procfile │
└──────┬───────┘
│
▼
┌──────────────┐
│ Git Commit │
│ git add . │
│ git commit │
└──────┬───────┘
│
▼
┌──────────────┐
│ Git Push │
│ git push │
│ heroku main │
└──────┬───────┘
│
▼
┌──────────────┐
│ Buildpack │
│ 检测语言 │
│ 安装依赖 │
└──────┬───────┘
│
▼
┌──────────────┐
│ 创建 Slug │
│ 打包应用 │
└──────┬───────┘
│
▼
┌──────────────┐
│ 创建 Release│
│ Slug + Config│
└──────┬───────┘
│
▼
┌──────────────┐
│ 启动 Dyno │
│ 运行应用 │
└──────────────┘
故障排查 #
部署失败 #
bash
# 查看详细错误信息
heroku logs --tail
# 常见错误及解决方案
# 1. Buildpack 未检测到
# 确保有 package.json 文件
# 或手动指定 buildpack
heroku buildpacks:set heroku/nodejs
# 2. 端口问题
# 确保使用 process.env.PORT
const PORT = process.env.PORT || 3000;
# 3. 依赖安装失败
# 检查 package.json 格式
# 确保版本号正确
应用崩溃 #
bash
# 查看 Dyno 状态
heroku ps
# 查看日志
heroku logs --tail
# 重启应用
heroku restart
# 常见原因:
# - 未绑定端口
# - Procfile 格式错误
# - 依赖缺失
# - 环境变量未设置
无法访问 #
bash
# 检查 Dyno 是否运行
heroku ps
# 确保 Web Dyno 数量 > 0
heroku ps:scale web=1
# 检查路由
heroku routes
下一步 #
恭喜你完成了第一个应用的部署!接下来学习 核心概念详解 深入了解 Heroku 的工作原理。
最后更新:2026-03-28