作成: Lab2期 2016/12/9
更新: Lab3期 2017/7/20
授業チュートリアル
- 基本記事ベースです
- 事前に公開できる場合はします
- 毎回、宿題を考えてくるので宿題をやってみましょう
- わからないことは https://www.facebook.com/sugawara.ryousuke まで(基本G's生限定)
自己紹介
のびすけ
関連資料
Node.jsの調べ方
基本
- 公式: https://nodejs.org/ja/
- 日本公式: http://nodejs.jp/
- イベント: http://nodejs.connpass.com
- カンファレンス: http://nodefest.jp
誰を追えばいいのか
まずは会長情報をウォッチしよう
その他情報の調べ方
- Qiita: http://qiita.com/tags/Node.js
- teratail: https://teratail.com/tags/Node.js
- アドベントカレンダー: http://qiita.com/advent-calendar/2016/nodejs
- Slack: https://iojs-jp-slack.herokuapp.com
その他、技術調べるときによく使うサイト
- HTML5Experts.jp: https://html5experts.jp/
- CodeZine: http://codezine.jp/
- CodeIQ Magazine: https://codeiq.jp/magazine/
- gihyo.jp: https://gihyo.jp/
- はてなテクノロジータグ: http://b.hatena.ne.jp/hotentry/it
- LIG: https://liginc.co.jp
- dotstudio: https://dotstud.io
Webの仕組み
- http://www.system-dev.jp/beans/001.html
- http://nyumon-info.com/web/websystem.html
- Webの仕組みとWebサーバの構造 (1/2)
- HTML初心者必見!Webページの仕組みをわかりやすく説明!
- 応用: 【翻訳】リッチなWebアプリケーションのための7つの原則
- Webの動向など(別資料)
- Webシステム
- クライアントとサーバー
- IPアドレス
- HTTP/HTTPS/HTTP2
- GET/POST
Node.jsとは
- JavaScriptについて: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Introduction
- node.jsとはなにか: http://d.hatena.ne.jp/badatmath/20101020/1287587240
- wikipedia: https://ja.wikipedia.org/wiki/Node.js
- Node.js入門: https://gist.github.com/meso/6805914
- JavaScriptBootcamp.pdf
- ナルトと殺せんせー
- c10k問題
- ノンブロッキングI/O
-
環境構築
-
エディタ: 基本何でもいいです。
- 強い人たち -> vimやemacs
- Node.js界隈で人気なイメージ
- atom: https://atom.io/
- Visual Studio Code: https://code.visualstudio.com/
ターミナル: macのデフォルトでもいいけど iTerm2おすすめ
インストールとハローワールド
- nodebrew版(推奨): 僕がMacを買い換えてすぐにいれるツール&すぐにする初期設定【2016年9月版】
- nvm版: LIG
curl -L git.io/nodebrew | perl - setup
echo "export PATH=$HOME/.nodebrew/current/bin:$PATH" >> .bash_profile
source .bash_profile
最新版インストール
最新版の確認 https://nodejs.org/ja/
現時点2017/
nodebrew install-binary 8.2.0
nodebrew use 8.2.0
node -v
v8.2.0
はじめかた
npm init -y
app.js
'use strict'
const http = require('http');
const PORT = 3000;
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
res.end('Hello Node.js\n');
}).listen(PORT);
console.log(`Server running at http://localhost:${PORT}`);
-
http://localhost:3000 にアクセスすると
Hello Node.js
と表示される
終わるときはcontroll+c
たぶんこの辺から午後
ルーティングについて
app.js
'use strict'
const http = require('http');
const PORT = 3000;
console.log("こんにちは");
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
console.log(req.url);
if(req.url === '/kodama'){
res.end('せかいをかえるぎーくに\n');
}else if(req.url === '/kuribayashi'){
res.end('JSさいこー!\n');
}else{
res.end('Hello Node.js\n');
}
}).listen(PORT);
console.log(`Server running at http://localhost:${PORT}`);
変数、演算子、制御構造
- 変数: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Grammar_and_Types
- 式と演算子: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Expressions_and_Operators
- 比較演算子: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
npmについて
npm init --yes
npm i request
フォーム利用
ハンズオン
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title>ログイン</title>
</head>
<body>
<form method="post" action="/post">
<p>ユーザID:<input type="text" name="userid"></p>
<p>パスワード:<input type="password" name="password"></p>
<p><input type="submit"></p>
</form>
</body>
</html>
- フォームを表示させた時点のコード
app.js
'use strict'
const http = require('http');
const PORT = 3000;
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
res.end(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title>ログイン</title>
</head>
<body>
<form method="post" action="/post">
<p>ユーザID:<input type="text" name="userid"></p>
<p>パスワード:<input type="password" name="password"></p>
<p><input type="submit"></p>
</form>
</body>
</html>`);
}).listen(PORT);
console.log(`Server running at http://localhost:${PORT}`);
- server.js
server.js
'use strict'
const http = require('http');
const PORT = 9999;
http.createServer((req, res) => {
console.log(req.url);
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('hello');
}).listen(PORT);
console.log(`Server running at http://localhost:${PORT}`);
宿題
ログイン用のテキストフォームに情報をいれて送信すると、サーバー側で情報を受け取って表示ができる状態にしましょう!
expressをつかっても使わなくてもok