LoginSignup
18
19

More than 5 years have passed since last update.

Node.jsアプリケーションをHerokuにデプロイ

Posted at

事前準備

  1. node.jsをインストール
    https://nodejs.org/ja/

  2. gitをインストール
    https://git-scm.com/downloads

  3. herokuに登録
    https://id.heroku.com/login

  4. Heroku Toolbeltをインストール
    https://devcenter.heroku.com/articles/heroku-cli

アプリ

ディレクトリ構成
[app-dirctory]
   │
   ├─app.js
   │
   └─Procfile
app.js
var http = require('http'); //httpモジュール呼び出し
var server = http.createServer(function (request, response) {
    // リクエストを受けると以下のレスポンスを送信する
    response.writeHead(200, {'Content-Type': 'text/plain'}); //レスポンスヘッダーに書き込み
    response.write('Hello World\n'); // レスポンスボディに「Hello World」を書き込み
    response.end(); // レスポンス送信を完了する
});
server.listen(process.env.PORT || 8080);  //8080番ポートで待ち受け
Procfile
web: node app.js

サーバーの待受ポートはheroku上で勝手に決められるためこういった設定にしないといけない
ローカル実行した場合には8080ポートが使用される
server.listen(process.env.PORT || 8080);

コマンドライン操作

  1. package.json作成
    npm init
  2. gitリポジトリを作成
    git init
  3. gitリポジトリにアプリ追加
    git add --a
  4. gitリポジトリにコミット
    git commit -m "commit"
  5. herokuアプリ追加
    heroku create
    または
    heroku create アプリケーションネーム
  6. herokuにプッシュ
    git push heroku master
  7. アプリを開く
    heroku open

Hello Worldが出れば成功!
image.png

18
19
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
18
19