LoginSignup
123

More than 5 years have passed since last update.

Node.jsをHerokuへデプロイ

Last updated at Posted at 2014-04-20

heroku toolbeltのインストール

heroku toolbeld(heroku推奨)
https://toolbelt.heroku.com/

herokuコマンドと同時にrubyとgitが同時にインストールされる。

すでにRubyや、windowsでmsysgitをインストールしていると、rubyやgitのバージョンが下がることもある。

すでにRubyをインストールしている場合は

$ gem install heroku

herokuツールベルトのバージョン確認

$ heroku version

herokuコマンドのhelp

$ heroku help

herokuにログイン

herokuに登録したEメールとパスワードをきかれる

$ heroku login

sshキーが使用中のPCアカウントになければsshキーの生成

$ ssh-keygen

windowsではssh-keygenコマンドは実行できないが、msysgitを使えば可能

herokuにssh公開鍵をアップロード

$ heroku keys:add ~/.ssh/id_rsa.pub

herokuの管理画面からでも可能

Herokuにからアプリをコマンドから生成

cd アプリのパス

git init

git status

herokuに空アプリ生成

heroku create アプリ名(半角英数字記号ハイフンのみ)

※heroku createだけだと勝手にアプリ名が付けられる

package.jsonに使用するモジュールを記述

※Expressを使用する場合はpackage.jsonのファイルは自動生成される

package.json
{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },

  "engine": {
    "node": "v0.10.x",
    "npm": "1.3.x"
  },
  "dependencies": {
    "express": "3.3.4",
    "ejs": "*",
    "mongodb": "*" //←使用するモジュールを追加しておく
  }
}

参考
Node.jsのバージョン確認
node -v
npmコマンドのバージョン確認
npm -v

Procfile作成

アプリを起動するコマンドを指定(※拡張子なし)

Procfile
web: node app.js 

CoffeeScriptで起動する場合

Procfile
web: node app.coffee

デプロイする前にコミットしておく

cd myapp

git init

git add Procfile *.js *.json public views routes

git commit -m "コミットメッセージ"

git tag v1.0

node_moduleは.gitignoreに登録

.gitignore
node_modules
.
.

リモートリポジトリ追加

※heroku create app名のときに自動的に追加されている

git remote add heroku git@heroku.com:アプリ名.git

リモートリポジトリ削除

git remote remove heroku

herokuにpush

git push heroku master

アプリを既定のブラウザで起動

heroku open

更新

git add .

git commit -m "message"

git push heroku master

heroku run rake db:migrade

heroku run rake db:seed

package.json
{
        "name": "sample03",
        "version": "0.0.1",
        "private": true,
        "scripts" : {
                "start": "node app"
        }
        "engines": {
                "node": "0.8.x",
                "npm": "1.1.x"
        }
        "dependencies": {
                "express": "3.0.0beta7",
                "jade": "*"
        }
}

Procfile

web: node app.js

foreman start

ローカルでアプリケーションが実行できるか確認するコマンド

ctrl + c

heroku apps:info

heroku logs

heroku apps:destroy

heroku apps:destroy -a node-sample-2012-0922 --confirm node-sample-2012-0922

参考
Herokuで作るFacebookアプリ
http://gihyo.jp/dev/serial/01/heroku

heroku再起動
heroku restart

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
123