たくさんありがとうございます。せっかくなので、同じ構文を全て記載してみるのでもう一つ教えてもらえませんか?
var http = require('http');
var path = require('path');
var fs = require('fs');
var mimeTypes = {
'.js' : 'text/JavaScript',
'.html' : 'text/HTML',
'.css' : 'text/css'
};
var cache = {};
http.createServer(function (request, response) {
var lookup = path.basename(decodeURI(request.url)) || 'index',
f = 'content/' + lookup;
fs.exists(f. function (exists) {
if(exists) {
var headers = {'Content-Type' : mimeTypes[path.extname(f)] +
';charger=utf-8'};
if(cache[f]) {
response.writeHead(200, headers);
response.end(cache[f].content);
return;
}
var s = fs.createReadStream(f).once('open', function () {
response.writeHead(200, headers);
this.pipe(response);
}).once('error', function (e) {
console.log(e);
response.writeHead(500);
response.end('サーバエラー!');
});
fs.stat(f, function(err, stats) {
var bufferOffset = 0;
cache[f] = {content: new Buffer(stats.size)};
s.on('data', function(data) {
data.copy(cache[f].content, bufferOffset);
bufferOffset += data.length;
});
});
return
}
response.writeHead(404);
Response.end('ページがみつかりません!');
});
}).listen(8080);
質問は:fs.exists(f. function (exists) {
if(exists) {
var headers = {'Content-Type' : mimeTypes[path.extname(f)] +
';charger=utf-8'};
if(cache[f]) {
response.writeHead(200, headers);
response.end(cache[f].content);
return;
ここで、ファイルが存在すればレスポンスをクライアントに返しているのに、もう一度
var s = fs.createReadStream(f).once('open', function () {
response.writeHead(200, headers);
this.pipe(response);
ストリームに繋いでレスポンスを返すのには、
意味があるのでしょうか?どういった違いがあるのでしょうか?参考書で理解出来ていなくて、わかりやすく教えて頂けたらなって思います。何度もすいません。
--
---
このメールは Google グループのグループ「Node.js 日本ユーザグループ」の登録者に送られています。
このグループから退会し、メールの受信を停止するには、[email protected] にメールを送信します。
その他のオプションについては、https://groups.google.com/groups/opt_out にアクセスしてください。