0
모듈 NET (소켓 포함), 을 통해 내 정적 웹 서버를 만들고 텍스트 파일 (html, js, css ...)에 대한 응답을 쓰고 싶습니다. img 파일 (jpg, gif ....)에 대한 응답,Node.js에서 정적 파일 읽기
나는 createReadStream 및 readFile을 사용하여 작업을 시도했지만 정상적으로 작동했지만 항상로드 시간이 길어져 사진이 작동하지 않는 경우가 있습니다. ..
FS.stat(fileSystemAddress + request.fileName, function (err, stat) {
if (err) {
// Error handling
} else {
if (typeImg[request.fileType] != undefined) {
response.headers['Content-Length'] = stat.size;
response.headers['Content-Type'] = typeImg[request.fileType];
response.writeImg(200);
var fileStream = FS.createReadStream(fileSystemAddress + request.fileName);
fileStream.pipe(response.socket, {
end: false
});
}
}
});
if (typeFile[request.fileType] != undefined) {
FS.readFile(fileSystemAddress + request.fileName, 'utf8', function (err,data) {
if (err) {
// Error handling
} else {
response.headers['Content-Length'] = data.length;
response.headers['Content-Type'] = typeFile[request.fileType];
response.writeFile(200,data);
}
});
}
과의 WriteFile, writeImg 방법 :
this.writeImg = function (code) {
this.status(code);
this.title = that.protocol + "/" + that.httpVersion + " " + that.statusCode.name + " " + that.statusCode.message + "\r\n";
this.socket.write(that.title + that.responseTime + "Content-Type:" + that.headers["Content-Type"] + "\r\n" + "Content-Length: " + that.headers["Content-Length"] + "\r\n" + "\r\n");
}
this.writeFile = function(code,body){
this.status(code);
this.title = that.protocol + "/" + that.httpVersion + " " + that.statusCode.name + " " + that.statusCode.message + "\r\n";
console.log(that.title + that.responseTime + "Content-Type:" + that.headers["Content-Type"] + "\r\n" + "Content-Length: " + that.headers["Content-Length"] + "\r\n" + body);
this.socket.write(that.title + that.responseTime + "Content-Type:" + that.headers["Content-Type"] + "\r\n" + "Content-Length: " + that.headers["Content-Length"] + "\r\n" + "\r\n" + body);
}
로딩 할 때 웹에서 파일이나 이미지를 관리하기 위해 할 수있는 일은 무엇입니까? (로컬 서버는 그렇게 빨리 작동해야합니다.) 감사합니다.
내가 표현에 대해 알고 있지만, 내가 말했듯이, 모듈 NET을 통해 내 자신의 정적 웹 서버를 구축하려고 해요 :) 감사합니다. – Magiclog