LoginSignup
34
34

More than 5 years have passed since last update.

Herokuに手っ取り早く静的サイトをデプロイする(node版)

Last updated at Posted at 2014-07-02

index.htmlをデプロイするだけなら 'touch index.php;', 'echo 'php_flag engine off' > .htaccess' が最速なんだけど、ルートディレクトリに静的アセットを起きたくないという理由でなんか嫌だった。

Rubyの人ならrack upするアプリ書くんだろうけど、nodeでビルドツール使いまくったリポジトリなので、nodeでやる。

npmとrubyとheroku toolbelt が入ってるのを前提。

環境構築

npmからexpressだけ入れとく。

npm init # package.jsonがないなら
npm install express --save

package.jsonに追記

  "engines": {
    "node": "0.10.x"
  },

node v0.10系を使えという指定

server.js

var express = require('express')
  , http = require('http')
  , app = express()
  ;
app.use(express.static(__dirname + '/public'));
var port = process.env.PORT || 5000;
var server = http.createServer(app).listen(port);

buildのところがホストしたいindex.htmlを含むディレクトリ

Procfile

web: node server.js

Foreman 入れて動作確認する

gem install foreman
foreman start

localhost:5000で動いれてればおk

ディレクトリはたぶんこうなってる

- .git
- .gitignore
- package.json
- server.js
- public/ # index.html等、ホストするファイルを含むディレクトリ
- Procfile

node_modulesとかは適宜.gitignore してください

デプロイ

git init; git add .; git commit -m"init" # もしやってないなら
heroku create 
git push heroku master
heroku ps:scale web=1 # dynoの数を1 に
heroku open # ブラウザで開く

動いた

34
34
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
34
34