Posted at

heroku + nodejs

More than 3 years have passed since last update.


step

Getting Started with Node.js on Heroku にそってやってみる


Heroku Toolbelt

Setup のセクションでDownload & install の手順があるのでその通りに行う。

コマンドラインからら heroku コマンドが実行できれば準備ok

$ heroku

Usage: heroku COMMAND [--app APP] [command-specific-options]
...

以下を実行する

$ heroku login

Enter your Heroku credentials.
Email: hiyuzawa@gmail.com
Password (typing will be hidden):
Authentication successful.

サンプルとして提供されているnodejsアプリを git clone する

git clone https://github.com/heroku/node-js-getting-started.git

$ heroku create

Creating gentle-reaches-3456... done, stack is cedar-14
https://gentle-reaches-3456.herokuapp.com/ | https://git.heroku.com/gentle-reaches-3456.git
Git remote heroku added
$
$ git remote -v
heroku https://git.heroku.com/gentle-reaches-3456.git (fetch)
heroku https://git.heroku.com/gentle-reaches-3456.git (push)
origin https://github.com/heroku/node-js-getting-started.git (fetch)
origin https://github.com/heroku/node-js-getting-started.git (push)
$
$ git push heroku master
Counting objects: 375, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (295/295), done.
Writing objects: 100% (375/375), 208.74 KiB | 0 bytes/s, done.
Total 375 (delta 50), reused 375 (delta 50)
...
...
$ heroku ps:scale web=1
Scaling dynos... done, now running web at 1:1X.
$ heroku open

ブラウザが立ち上がってsmpele app がデプロイれているのをブラウザで確認


tips


  • tail

$ heroku log --tail

2014-12-12T11:10:13.958570+00:00 heroku[router]: at=info method=GET path="/" host=gentle-reaches-3456.herokuapp.com request_id=005ea777-e7b0-4e6a-a659-e291afc2d634 fwd="126.54.29.38" dyno=web.1 connect=1ms service=3ms status=304 bytes=130


  • apps:info

 heroku apps:info

=== gentle-reaches-3456
Dynos: 1
Git URL: https://git.heroku.com/gentle-reaches-3456.git
Owner Email: hiyuzawa@gmail.com
Region: us
Repo Size: 211k
Slug Size: 5M
Stack: cedar-14
Web URL: https://gentle-reaches-3456.herokuapp.com/
Workers: 0
updating...done. Updated to 3.20.0


  • ps

$ heroku ps

=== web (1X): `node index.js`
web.1: up 2014/12/12 20:09:48 (~ 9m ago)


  • maintenance

$ heroku maintenance:on

Enabling maintenance mode for gentle-reaches-3456... done


  • run

$ heroku run bash

Running `bash` attached to terminal... up, run.4882
~ $
~ $ ls
Procfile README.md app.json index.js node_modules package.json public vendor
~ $ exit
$


Procfile

アプリケーションRootにある Procfile で

$ cat Procfile

web: node index.js


releases

$ heroku releases

=== gentle-reaches-3456 Releases
v4 Deploy 8c8b5ac hiyuzawa@gmail.com 2014/12/12 20:33:15 (~ 1m ago)
v3 Deploy 1adc26f hiyuzawa@gmail.com 2014/12/10 23:25:32
v2 Enable Logplex hiyuzawa@gmail.com 2014/12/10 23:22:44
v1 Initial release hiyuzawa@gmail.com 2014/12/10 23:22:43
$
$ heroku release:rollback v3


環境変数

$ heroku config:set name=hiyuzawa

Setting config vars and restarting gentle-reaches-3456... done, v9
name: hiyuzawa
$ heroku config:get name
hiyuzawa
$ heroku config
=== gentle-reaches-3456 Config Vars
name: hiyuzawa

これでnodejsから process.env.name で参照できる

ローカル開発環境にてこのenvを参照するには foreman を使う。

$ more .env

name=hiyuzawa
$ foreman start
20:52:20 web.1 | started with pid 26689
20:52:21 web.1 | Node app is running at localhost:5000

.gitignore 追加も忘れずに

$ cat .gitignore

node_modules
.env