Node予備校 vol.1 名古屋

46
Node.js with Heroku @sngmr

description

@sngmrのスライド

Transcript of Node予備校 vol.1 名古屋

Page 1: Node予備校 vol.1 名古屋

Node.js with Heroku

@sngmr

Page 2: Node予備校 vol.1 名古屋

自己紹介

Page 3: Node予備校 vol.1 名古屋

Shingo Mori森 真 吾ConnectionWorks Inc.(株)コネクションワークス

Twitter: @sngmr

ソフトウェアエンジニア兼営業兼企画兼コンサル兼経理

Titanium Chatroom Nagoya

Page 4: Node予備校 vol.1 名古屋

今日のはなしは・・・

Page 5: Node予備校 vol.1 名古屋

やってみたレベルです

Page 6: Node予備校 vol.1 名古屋

あと・・・

Page 7: Node予備校 vol.1 名古屋

Mac前提です

Page 8: Node予備校 vol.1 名古屋

Herokuってなに?

Page 9: Node予備校 vol.1 名古屋

•PaaS•Platform As A Service•Rails•Node.js from June 2011•

What’s Heroku?

Page 10: Node予備校 vol.1 名古屋

‣Herokuアカウント‣Macで動くNode‣Homebrewとかで

‣NPM(Node Package Manager)‣curl http://npmjs.org/install.sh | sh

‣Heroku Toolbelt‣Heroku Clientなどがバンドル‣https://toolbelt.heroku.com/

‣Gitの基礎知識

なにがいるの?

Page 11: Node予備校 vol.1 名古屋

さっそくやってみよう

Page 12: Node予備校 vol.1 名古屋

# Herokuはコマンド経由で操作することが多い

$ heroku login

Enter your Heroku credentials.

Email: [email protected]

Password (typing will be hidden):

Authentication successful.

Herokuログイン

Page 13: Node予備校 vol.1 名古屋

# ssh公開鍵が普通の場所にあれば、このコマンドで勝手に送ってくれる

$ heroku keys:add

Found existing public key: /Users/hoge/.ssh/id_rsa.pub

Uploading SSH public key /Users/hoge/.ssh/id_rsa.pub

ssh公開鍵の登録

Page 14: Node予備校 vol.1 名古屋

さぁ Hello World だ

Page 15: Node予備校 vol.1 名古屋

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

app.get('/', function(request, response) { response.send('Hello World!');});

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

web.js

Page 16: Node予備校 vol.1 名古屋

// 依存関係の定義

{ "name": "hnatest",

"version": "0.0.1", "dependencies": {

"express": "" // バージョン番号 }}

package.json

Page 17: Node予備校 vol.1 名古屋

$ npm installnpm http GET https://registry.npmjs.org/express

npm http 200 https://registry.npmjs.org/express

npm http GET https://registry.npmjs.org/express/-/express-2.5.9.tgz

・・・[email protected] ./node_modules/express

├── [email protected]├── [email protected]

├── [email protected]└── [email protected] ([email protected])

依存関係のインストール

Page 18: Node予備校 vol.1 名古屋

HerokuへのアップロードはGitを使います

Page 19: Node予備校 vol.1 名古屋

$ echo node_modules >> .gitignore$ ls -la

total 24

drwxr-xr-x 6 hoge staff 204 5 20 10:42 .

drwxr-xr-x 11 hoge staff 374 5 20 10:16 ..

-rw-r--r-- 1 hoge staff 24 5 20 10:42 .gitignore

drwxr-xr-x 4 hoge staff 136 5 20 10:36 node_modules

-rw-r--r-- 1 hoge staff 90 5 20 10:36 package.json

-rw-r--r-- 1 hoge staff 276 5 20 10:18 web.js

.gitignore

Page 20: Node予備校 vol.1 名古屋

web: node web.js

Procfile

Page 21: Node予備校 vol.1 名古屋

動かしてみよう!

Page 22: Node予備校 vol.1 名古屋

$ foreman start

11:06:56 web.1 | started with pid 603711:06:56 web.1 | Listening on 5000

foreman start

Page 23: Node予備校 vol.1 名古屋
Page 24: Node予備校 vol.1 名古屋

$ git init

$ git add .$ git commit -m "init"

gitリポジトリ作成

Page 25: Node予備校 vol.1 名古屋

heroku create --stack cedar

Herokuアプリ作成

Page 26: Node予備校 vol.1 名古屋

git push heroku master

Herokuへpush

Page 27: Node予備校 vol.1 名古屋
Page 28: Node予備校 vol.1 名古屋
Page 29: Node予備校 vol.1 名古屋
Page 30: Node予備校 vol.1 名古屋

簡単でしょ?

Page 31: Node予備校 vol.1 名古屋

Facebookアプリも楽

Page 32: Node予備校 vol.1 名古屋

https://devcenter.heroku.com/articles/nodejs

ここまでの話は…

Page 33: Node予備校 vol.1 名古屋

あとスライドは…

http://www.slideshare.net/MoriShingo/node-vol1

Page 34: Node予備校 vol.1 名古屋

せっかくMacに

Node.js入ったし…

Page 35: Node予備校 vol.1 名古屋

Node.js on local

@sngmr

Page 36: Node予備校 vol.1 名古屋

$ node

>

まずはコマンドで

Page 37: Node予備校 vol.1 名古屋

ローカルでよく使うモノ

Page 38: Node予備校 vol.1 名古屋

$ node arg.js hoge fuga[ 'node',

'/Users/shingo/Projects/Node/local_base/arg.js',

'hoge',

'fuga' ]

---- arg.js ----

console.log(process.argv);

コマンドライン引数

Page 39: Node予備校 vol.1 名古屋

$ cat /etc/hosts | node pipe1.js

---- pipe1.js ----process.stdin.resume();

process.stdin.setEncoding('utf8');process.stdin.on('data', function(data) {

process.stdout.write(data);});

ファイルインプット ver.1

Page 40: Node予備校 vol.1 名古屋

##

# Host Database

#

# localhost is used to configure the loopback interface

# when the system is booting. Do not change this entry.

##

127.0.0.1 localhost

255.255.255.255 broadcasthost

::1 localhost

fe80::1%lo0 localhost

・・・

Page 41: Node予備校 vol.1 名古屋

$ cat /etc/hosts | node pipe1.js > hosts.copy

---- pipe1.js ----process.stdin.resume();

process.stdin.setEncoding('utf8');process.stdin.on('data', function(data) {

process.stdout.write(data);});

ファイルアウトプット ver.1

Page 42: Node予備校 vol.1 名古屋

$ node readwrite.js

---- reaswrite.js ----var fs = require('fs');

var r = fs.createReadStream('/etc/hosts');var w = fs.createWriteStream('hosts2.copy');

r.on('data', function(data) { w.write(data.toString().toUpperCase());

});

インプットアウトプット

Page 43: Node予備校 vol.1 名古屋

どうゆう場面で使うの?

Page 44: Node予備校 vol.1 名古屋

‣例:‣CSVからJSONファイルを起こしたい‣正規表現だけだと苦しいデータとか

‣特に Titanium とは相性いいよ!

データ編集とか楽

Page 45: Node予備校 vol.1 名古屋

var fs = require('fs');

function readLines(input, func) { var remaining = ''; input.on('data', function(data) { remaining += data; var index = remaining.indexOf('\n'); while (index > -1) { var line = remaining.substring(0, index); remaining = remaining.substring(index + 1); func(line); index = remaining.indexOf('\n'); } }); input.on('end', function() { if (remaining.length > 0) { func(remaining); } });}

インプットアウトプット

Page 46: Node予備校 vol.1 名古屋

var input = fs.createReadStream('sample1.csv');var data = [];readLines(input, function(line) { var buf = line.split("\t"); data.push(buf); }, function() { console.log(data); });

インプットアウトプット