https://devcenter.heroku.com/categories/reference
https://devcenter.heroku.com/articles/git
首先,你需要下载git及heroku CLI
安装后需要先登录,会在浏览器中弹出heroku登陆界面
C:\nodejsmap>heroku login
heroku: Press any key to open up the browser to login or q to exit:
The following example demonstrates initializing a Git repository for an app that lives in the myapp
directory:
$ cd myapp
$ git init
Initialized empty Git repository in .git/
$ git add .
$ git commit -m "My first commit"
$ git push heroku master
每次修改代码后都要重新push heroku master
查看已安装的所有应用 all apps in a Private Space:
$ heroku apps --space my-space-name
=== Apps in space my-space-name
my-space-app-1
my-space-app-2
View info about an app in a Private Space
$ heroku info --app my-space-app
=== my-space-app
Collaborators: my-team-name@example.com
Git URL: https://git.heroku.com/my-space-app.git
Owner: my-team-name
Region: tokyo
Space: my-space-name
Stack: heroku-18
Web URL: http://my-space-app.herokuapp.com/
安装MYSQL数据库,选择ClearDB MySQL
https://devcenter.heroku.com/articles/cleardb
The ClearDB add-on can be installed and up and running in your app in no time! Run the following command to add ClearDB to your application:
$ heroku addons:create cleardb:ignite
-----> Adding cleardb to sharp-mountain-4005... done, v18 (free)
Retrieve your database URL by issuing the following command:
$ heroku config | grep CLEARDB_DATABASE_URL
CLEARDB_DATABASE_URL => mysql://adffdadf2341:adf4234@us-cdbr-east.cleardb.com/heroku_db?reconnect=true
mysql://bc6acf0a01b:24f1af2@us-cdbr-iron-east-02.cleardb.net/heroku_33a4354848f453?reconnect=true 里面包含了我们需要的数据库相关信息!
- 用户名 bc6acf0a01b
- 密码 24f1af2
- 数据库服务器地址 s-cdbr-iron-east-02.cleardb.net
- 数据库名称 heroku_33a4354848f453
Copy the value of the CLEARDB_DATABASE_URL
config variable.更新DATABASE_URL
$ heroku config:set DATABASE_URL='mysql://adffdadf2341:adf4234@us-cdbr-east.cleardb.com/heroku_db?reconnect=true'
Adding config vars:
DATABASE_URL => mysql2://adffd...b?reconnect=true
Restarting app... done, v61.
主要注意的是package.json中必须明确指明node.js版本及npm版本
{
"name": "nodejsmap",
"version": "1.0.0",
"description": "nodejsmap1.0",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mysql": "^2.17.1"
},
"engines": {
"node": "12.1.0",
"npm": "6.9.0"
}
}
需将原
server.listen(8080, '127.0.0.1', () => {
中更改为process.env.PORT变量,这样heroku会自动进行端口监听
server.listen(process.env.PORT || 8080 , () => {
//console.log(`server starting at ${config.host}:${config.port}`)
console.log(`server starting at 127.0.0.1:8080`)
})
非常有用的一个项目https://github.com/mysqljs/mysql#pooling-connections
主要是JavaScript使用mySql连接池等~~~~~~·
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
$ heroku logs --tail
查看运行日志,调试时使用