Node予備校 vol.1 名古屋
-
Upload
mori-shingo -
Category
Technology
-
view
4.841 -
download
1
description
Transcript of Node予備校 vol.1 名古屋
Node.js with Heroku
@sngmr
自己紹介
Shingo Mori森 真 吾ConnectionWorks Inc.(株)コネクションワークス
Twitter: @sngmr
ソフトウェアエンジニア兼営業兼企画兼コンサル兼経理
Titanium Chatroom Nagoya
今日のはなしは・・・
やってみたレベルです
あと・・・
Mac前提です
Herokuってなに?
•PaaS•Platform As A Service•Rails•Node.js from June 2011•
What’s Heroku?
‣Herokuアカウント‣Macで動くNode‣Homebrewとかで
‣NPM(Node Package Manager)‣curl http://npmjs.org/install.sh | sh
‣Heroku Toolbelt‣Heroku Clientなどがバンドル‣https://toolbelt.heroku.com/
‣Gitの基礎知識
なにがいるの?
さっそくやってみよう
# Herokuはコマンド経由で操作することが多い
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Authentication successful.
Herokuログイン
# 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公開鍵の登録
さぁ Hello World だ
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
// 依存関係の定義
{ "name": "hnatest",
"version": "0.0.1", "dependencies": {
"express": "" // バージョン番号 }}
package.json
$ 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])
依存関係のインストール
HerokuへのアップロードはGitを使います
$ 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
web: node web.js
Procfile
動かしてみよう!
$ foreman start
11:06:56 web.1 | started with pid 603711:06:56 web.1 | Listening on 5000
foreman start
$ git init
$ git add .$ git commit -m "init"
gitリポジトリ作成
heroku create --stack cedar
Herokuアプリ作成
git push heroku master
Herokuへpush
簡単でしょ?
Facebookアプリも楽
https://devcenter.heroku.com/articles/nodejs
ここまでの話は…
あとスライドは…
http://www.slideshare.net/MoriShingo/node-vol1
せっかくMacに
Node.js入ったし…
Node.js on local
@sngmr
$ node
>
まずはコマンドで
ローカルでよく使うモノ
$ node arg.js hoge fuga[ 'node',
'/Users/shingo/Projects/Node/local_base/arg.js',
'hoge',
'fuga' ]
---- arg.js ----
console.log(process.argv);
コマンドライン引数
$ 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
##
# 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
・・・
$ 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
$ 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());
});
インプットアウトプット
どうゆう場面で使うの?
‣例:‣CSVからJSONファイルを起こしたい‣正規表現だけだと苦しいデータとか
‣特に Titanium とは相性いいよ!
データ編集とか楽
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); } });}
インプットアウトプット
var input = fs.createReadStream('sample1.csv');var data = [];readLines(input, function(line) { var buf = line.split("\t"); data.push(buf); }, function() { console.log(data); });
インプットアウトプット