我需要知道我的想法是否合适。
我整理了一个流式传输html文件js文件css文件的Web服务器,这种静态Web服务器。
因为它一直是静态的,我认为我只需要真正读取文件,然后在开始时将其加载到数组中,以便在客户请求时减少文件读取。
像这种代码
var tttt = []
var server = http.createServer();
server.on("request",function(request, response){
var etret = '/home/bob/node-show/big-file.js';
var setHeader = function(file){
return {
'Content-Type': MIME.mime(file),
'Transfer-Encoding':'chunked',
'Connection': 'keep-alive'
};
}
if (!tttt[path.basename(etret)]) {
tttt[path.basename(etret)] = []
var test = fs.createReadStream(etret)
test.setEncoding('ascii')
test.on('data', function(data){
console.log(data)
tttt[path.basename(etret)].push(data)
});
test.on('close', function(data){
console.log('sadsad')
response.writeHead(200, setHeader(etret))
console.log(tttt[path.basename(etret)].length)
for (var i = 0; i < tttt[path.basename(etret)].length; i++) {
response.write(tttt[path.basename(etret)][i])
};
var erw = 0
for (var f = 0; f < tttt[path.basename(etret)].length; f++) {
erw = (erw + tttt[path.basename(etret)][f].length)
}
console.log(erw)
response.end()
});
}else{
response.writeHead(200, setHeader(etret))
console.log(tttt[path.basename(etret)].length)
for (var i = 0; i < tttt[path.basename(etret)].length; i++) {
response.write(tttt[path.basename(etret)][i])
};
var erw = 0
for (var f = 0; f < tttt[path.basename(etret)].length; f++) {
erw = (erw + tttt[path.basename(etret)][f].length)
}
console.log(erw)
response.end()
console.dir(server.connections)
}
//return _fontEnd(request, response);
}).listen(8000, function(){
console.log('HTTP server up');
});
big-file.js大概是500kb,将50个文件加载到内存中以保存硬盘读取是不是一个好主意?