node予備校 vol.1 名古屋
DESCRIPTION
@sngmrのスライドTRANSCRIPT
![Page 1: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/1.jpg)
Node.js with Heroku
@sngmr
![Page 2: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/2.jpg)
自己紹介
![Page 3: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/3.jpg)
Shingo Mori森 真 吾ConnectionWorks Inc.(株)コネクションワークス
Twitter: @sngmr
ソフトウェアエンジニア兼営業兼企画兼コンサル兼経理
Titanium Chatroom Nagoya
![Page 4: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/4.jpg)
今日のはなしは・・・
![Page 5: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/5.jpg)
やってみたレベルです
![Page 6: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/6.jpg)
あと・・・
![Page 7: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/7.jpg)
Mac前提です
![Page 8: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/8.jpg)
Herokuってなに?
![Page 9: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/9.jpg)
•PaaS•Platform As A Service•Rails•Node.js from June 2011•
What’s Heroku?
![Page 10: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/10.jpg)
‣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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/11.jpg)
さっそくやってみよう
![Page 12: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/12.jpg)
# Herokuはコマンド経由で操作することが多い
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Authentication successful.
Herokuログイン
![Page 13: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/13.jpg)
# 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/14.jpg)
さぁ Hello World だ
![Page 15: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/15.jpg)
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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/16.jpg)
// 依存関係の定義
{ "name": "hnatest",
"version": "0.0.1", "dependencies": {
"express": "" // バージョン番号 }}
package.json
![Page 17: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/17.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/18.jpg)
HerokuへのアップロードはGitを使います
![Page 19: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/19.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/20.jpg)
web: node web.js
Procfile
![Page 21: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/21.jpg)
動かしてみよう!
![Page 22: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/22.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/23.jpg)
![Page 24: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/24.jpg)
$ git init
$ git add .$ git commit -m "init"
gitリポジトリ作成
![Page 25: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/25.jpg)
heroku create --stack cedar
Herokuアプリ作成
![Page 26: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/26.jpg)
git push heroku master
Herokuへpush
![Page 27: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/27.jpg)
![Page 28: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/28.jpg)
![Page 29: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/29.jpg)
![Page 30: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/30.jpg)
簡単でしょ?
![Page 31: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/31.jpg)
Facebookアプリも楽
![Page 32: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/32.jpg)
https://devcenter.heroku.com/articles/nodejs
ここまでの話は…
![Page 33: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/33.jpg)
あとスライドは…
http://www.slideshare.net/MoriShingo/node-vol1
![Page 34: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/34.jpg)
せっかくMacに
Node.js入ったし…
![Page 35: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/35.jpg)
Node.js on local
@sngmr
![Page 36: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/36.jpg)
$ node
>
まずはコマンドで
![Page 37: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/37.jpg)
ローカルでよく使うモノ
![Page 38: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/38.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/39.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/40.jpg)
##
# 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/41.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/42.jpg)
$ 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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/43.jpg)
どうゆう場面で使うの?
![Page 44: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/44.jpg)
‣例:‣CSVからJSONファイルを起こしたい‣正規表現だけだと苦しいデータとか
‣特に Titanium とは相性いいよ!
データ編集とか楽
![Page 45: Node予備校 vol.1 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/45.jpg)
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 名古屋](https://reader034.vdocuments.site/reader034/viewer/2022052215/559222591a28abcd068b4604/html5/thumbnails/46.jpg)
var input = fs.createReadStream('sample1.csv');var data = [];readLines(input, function(line) { var buf = line.split("\t"); data.push(buf); }, function() { console.log(data); });
インプットアウトプット