Skip to content

Instantly share code, notes, and snippets.

@kysnm
Forked from shigeki/hello.js
Created December 3, 2012 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kysnm/4193217 to your computer and use it in GitHub Desktop.
Save kysnm/4193217 to your computer and use it in GitHub Desktop.
第1回Node.js入門勉強会 レポート課題
var http = require('http');
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
server.close();
});
server.listen(8080, 0, function () {
console.log('Server running at http://localhost:8080/');
});
名前(敬称略) 解答URL 課題1 課題2 server.closeの場所 課題2 ソケットの切断方法
外村 https://gist.github.com/4129163 ○ server.close()が二度呼ばれてエラーになるというのはnet.jsのコードを見ると確認できる(this._handleにnullが設定されるため) serverのconnectionイベント reqのendイベントで req.connection.end()
古川 https://gist.github.com/4134211#comments ○ console.logで2度 server.close が呼ばれていることを確認 serverのconnectionイベント serverのrequestイベントでreq.connection.destroy()
Toshiya SAITO https://gist.github.com/4135125 connectionイベント HTTPのレスポンスヘッダにConnection: closeを追加するか, レスポンスを返した後にHTTPリクエストのソケットを閉じる.
岩本 https://gist.github.com/4135345 カンニング? ○ connectionイベント reqのendイベントで req.connection.end()
野田 https://gist.github.com/4133802 ○(だだし「server.close()するよりも先に2つ目のrequestに対するhttp requestイベントの発火の方が早いがため」の記載が微妙) connectionイベント requestイベントで req.socket.end()
松尾 https://gist.github.com/4129036 ○ GJ! telnetでHTTP/1.0,1.1の違いを確認 connectionイベント レスポンスヘッダーに Connection: close を記入
土田 https://gist.github.com/4139254 ○ console.logで2度 server.closeが呼ばれていることを確認 connectionイベント requestイベントでreq.connection.end()
村山 https://gist.github.com/4189468 ▲ 「ところが、1回目のコールバックですでにserver.close()が実行されているため、次のリクエストで実行されるreq.end()(の中で実行されるsocket.write())においてエラーが生じる。」 connectionイベント requestイベントでconnectionイベント
浜辺 https://gist.github.com/4131753 connectionイベント req の endイベントでreq.connection.destroy()を once 、 もしくはres.shouldKeepAlive = falseでKeep-Alive自体を無効にする。

第1回の勉強会で重要だった点の一つは server.close() の挙動でした。 今回は、この server.close() の挙動についての課題です。

課題

A君は、ブラウザで1回閲覧したら HelloWorld のWebサーバが終了するプログラムを作ろうと hello.js を作りました。 だけど Chrome でアクセスすると下のようなエラーになってしまい、プログラムが正常に終了しません。

課題1

なぜエラーが発生したのかその理由を記述しなさい。

課題2

このプログラムを修正してエラーが発生せずA君がやりたかったプログラムを作りなさい。

提出方法

gist か ブログかに記載して、URLをメーリングリストに連絡してください。

##提出期限

次回勉強会開催まで。(その前に出そろえば答え合わせでもしましょうか)

> node hello.js
Server running at http://localhost:8080/

net.js:1046
    throw new Error('Not running');
          ^
Error: Not running
    at Server.close (net.js:1046:11)
    at Server.<anonymous> (/home/ohtsu/hello.js:5:10)
    at Server.EventEmitter.emit (events.js:99:17)
    at HTTPParser.parser.onIncoming (http.js:1807:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
    at Socket.socket.ondata (http.js:1704:22)
    at TCP.onread (net.js:403:27)
@kysnm
Copy link
Author

kysnm commented Dec 3, 2012

課題1

/favicon.ico へのアクセスが発生し、server.close() が二回呼び出される為

課題2

var http = require('http');
server = http.createServer(function (req, res) {
  if (req.url == '/') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
    server.close();
  } else {
    req.connection.destroy();
  }
});
server.listen(8080, 0, function () {
  console.log('Server running at http://localhost:8080/');
});

すいません、ちょっとカンニング気味かもです。。。

@kysnm
Copy link
Author

kysnm commented Dec 3, 2012

恥ずかしい、ものすごい間違えてますね。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment