2013/01/06

[heroku][node.js][Express]Deployまでの準備

node.jsもdeployしてみたので取り敢えずメモ
まーheorkuの公式に書いてあることをやってみただけ。。。

用意したのは下記4ファイル(expressコマンド -e で作成し必要なファイルのみ抜粋)
・views - index.ejs
・package.json
・Procfile
・web.js


中身はこんな感じ


<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
  </body>
</html>

{
  "name": "test-ex",
  "version": "0.0.1",
  "dependencies": {
    "express": "2.5.x",
    "ejs": "*"
  },
  "engines": {
    "node": "0.8.x",
    "npm": "1.1.x"
  }
}
web: node web.js
※Herokuでnode.jsを動かす際にはProcfileを用意して、起動jsを記述する必要がある。
公式参照
var express = require('express');

var app = express.createServer(express.logger());

app.get('/', function(request, response) {
  //response.send('Hello World!');
  //せっかくなのでテンプレの内容を表示
  response.render('index.ejs', { title: 'Express' , layout: false });
});

var port = process.env.PORT || 5000;
app.listen(port, function() {
  console.log("Listening on " + port);
});


上記を用意した後、下記git、herokuコマンドを実施
>git init

>git add .

>git commit -m "1st commit"

>heroku create test-ex  ← git initをやった後の方が楽

>git push heroku master

>heroku open ← ブラウザが起動しデプロイしたWebアプリが表示される





0 件のコメント:

コメントを投稿