NodeJSにHTTPを利用してDomain Socketをリスナーする

1.参考コード
var http = require('http’);
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type’: 'text/plain’});
res.end('welcome to arkgame.com\n’);
}).listen('/tmp/node_http.sock’);
console.log('Server running at /tmp/node_http.sock’);

2.テスト方法
telnet /tmp/node_http.sock
Trying /tmp/node_http.sock…
Connected to (null).
Escape character is '^]’.
GET / HTTP/1.1
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Mon, 26 Jan 2014 04:21:09 GMT
Connection: keep-alive
Transfer-Encoding: chunked
c
welcome to arkgame.com
0

3. NodeJSのHTTP Clientを利用してアクセス
var http = require('http’);
var options = {
socketPath: '/tmp/node_http.sock’,
method: 'GET’,
path: '/’
};
var req = http.request(options, function(res){
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.on('data’, function (chunk){
console.log(chunk.toString());
});
});
req.end();
4.http_client.jsのソースコードを実行
node http_client.js
STATUS: 200
HEADERS: {“content-type":"text/plain","date":"Mon, 26 Jan 2014 04:25:49 GMT","connection":"close","transfer-encoding":"chunked"}
welcome to arkgame.com

JavaScript

Posted by arkgame