LoginSignup
14
14

More than 5 years have passed since last update.

Node.js(Express)をHeroku上で動作させてみる

Last updated at Posted at 2014-11-21

今回やりたいこと

Node.jsのアプリケーションをHeroku上で動かしたかったので調べてみた。

$ express -e expressTest
$ cd expressTest
$ git init
$ heroku create
$ echo web: node ./bin/www > Procfile
$ git add .
$ git commit -m "initial commit"
$ git push heroku master
$ heroku open

上記をコマンドをエラーなく実行できればherokuでnode.jsアプリを動かせる。そこにたどり着くために調べたことを以下にまとめた。

前提条件

  • Herokuのユーザ登録
  • node.js : 0.10.26
  • npm : 1.4.3

Heroku コマンドのインストール

macの場合

以下のURLからheroku toolbeltをダウンロードしてインストールするだけ
https://toolbelt.heroku.com/

gitやrubyなどが同時にインストールされるらしい。既にgitやrbenvが入っていたがとりあえず動いている。

Linuxの場合

$ wget -qO- https://toolbelt.heroku.com/install.sh | sh
$ vi bash_profile

# add
PATH=$PATH:/usr/local/heroku/bin

上記コマンドを実行すると、.profileにPathを足そうとする?が、なかったのでbash_profileに追加した。追加する場所は環境に合わせて実施。

Getting Started with Node.js on Heroku を試してみる

以下のサイトをもとにとりあえずコマンドを試してみる。
https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction

  • サンプルプロジェクトのクローン
$ git clone https://github.com/heroku/node-js-getting-started.git
$ cd node-js-getting-started.git
  • heroku アプリ作成
$ heroku create --http-git
  • herokuへのデプロイ
$ git push heroku master
  • herokuの実行
$ heroku open
  • logの監視
$ heroku logs -tail
  • プロセスの確認
$ heroku ps 
  • Localでの実行
$ foreman start web
  • ランタイム実行(遅い?)
$ heroku run node

Heroku + Node.js + Githubを試してみる

自分のgithubで作成したリポジトリをHeroku上で動かしてみる。

ローカルとAWS上でNode.js + Expressを動かしてみる

  • githubでリポジトリの作成 リポジトリ名:heroku-nodeTest

Nodeプロジェクトの作成(Express + EJS)

  • express-generatorのインストール
$ npm install express-generator -g
  • プロジェクトの生成とgithubへのPush
$ express -e heroku-nodeTest
$ cd heroku-nodeTest
$ git init
$ git remote add origin https://github.com/mettoboshi/heroku-nodeTest.git
$ git add .
$ git commit -m "initial commit"
$ git push
  • 動作確認
$ npm install
$ DEBUG=heroku-nodeTest ./bin/www
or
$ npm start

動作確認。
http://localhost:3000/

ついでにgitignoreも追加しておく。

$ vi .gitignore

# add 
node_modules

$ git add .
$ git commit -m "add .gitignore"
$ git push

Herokuとの連携

  • Procfile

Procfileが必要なので追加。

$ vi Procfile

#add
web: node ./bin/www

ローカルで動くことを確認

$ foreman start web

http://localhost:5000/

  • herokuと連携させる

heroku keys:add ~/.ssh/KEYFILE

$ heroku create
$ heroku login
$ git push heroku master

ここでエラーが発生

エラー内容
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
$ ssh -vT git@heroku.com

どうも予定と違うKeyを使っているっぽいのでKeyを削除してあげ直した

# Keyがちゃんと消えていることの確認
$ heroku keys
  • アプリケーションをHerokuにPush
$ heroku keys:add
$ git push heroku master
  • 動作確認
$ heroku open

動きました。

Herokuボタンの設置

折角なのでHerokuボタンもつけてみた

  • app.jsonの作成
$ npm install -g app.json
$ app.json init
  • githubのReadme.mdに以下を記載
[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)

するとこんなボタンが表示される。

Deploy

実際にリポジトリ一番最初のコマンドで作ったリポジトリにボタンを配置してみた。

簡単にできるもんですね。

参考資料

Node.jsをHerokuへデプロイ: http://qiita.com/yoh-nak/items/80e51197410c7f956ccd
Heroku の Toolbelt をインストール: http://d.hatena.ne.jp/paraches/20130506/1367851247

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14