Javascript 错误:ENOENT,没有这样的文件或目录 Node JS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26069979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: ENOENT, no such file or directory Node JS
提问by writeToBhuwan
I am trying to upoad and download images to the server via Node.js and I am using the below code:
我正在尝试通过 Node.js 上传和下载图像到服务器,我正在使用以下代码:
var http = require('http'),
path = require('path'),
os = require('os'),
fs= require('fs'),url = require('url');
var Busboy = require('busboy');
http.createServer(function(req, res) {
if (req.method === 'POST') {
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
var saveTo = ".\Images\"+filename;
file.pipe(fs.createWriteStream(saveTo));
});
busboy.on('finish', function() {
res.writeHead(200, { 'Connection': 'close' });
res.end("That's all folks!");
});
return req.pipe(busboy);
}
else{
var request = url.parse(req.url, true);
console.log(request);
var action = request.pathname;
console.log(action);
if (action !== '/') {
var img = fs.readFileSync('.'+action);
res.writeHead(200, {'Content-Type': 'image/gif' });
res.end(img, 'binary');
} else {
res.writeHead(200, {'Content-Type': 'text/plain' });
res.end('Hello World \n');
}
}
res.writeHead(404);
res.end();
}).listen(8082, function() {
console.log('Listening for requests');
});
When I try to get the image from this server using HTTP GET at http://localhost:8082/images/betty.jpg, the request is fulfilled and the image is recieved but it also throws the error below:
当我尝试使用 HTTP GET at 从该服务器获取图像时http://localhost:8082/images/betty.jpg,请求已完成并收到图像,但它也会引发以下错误:
^
fs.js:438
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory 'D:\ImageUploadService\favicon.ico'
at Object.fs.openSync (fs.js:438:18)
at Object.fs.readFileSync (fs.js:289:15)
at Server.<anonymous> (D:\ImageUploadService\service.js:27:20)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2112:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23
)
at Socket.socket.ondata (http.js:1970:22)
at TCP.onread (net.js:527:27) 'D:\ImageUploadService\favicon.ico'
at Object.fs.openSync (fs.js:438:18)
at Object.fs.readFileSync (fs.js:289:15)
at Server.<anonymous> (D:\ImageUploadService\service.js:27:20)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2112:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23
)
at Socket.socket.ondata (http.js:1970:22)
at TCP.onread (net.js:527:27)
It seems that it is looking for some favicon.ico. What could be the problem??
似乎它正在寻找一些 favicon.ico。可能是什么问题呢??
回答by Dan D.
You need to check if the file exists with fs.existsSync(path)before attempting to read it:
fs.existsSync(path)在尝试读取文件之前,您需要检查文件是否存在:
if (action !== '/' && fs.existsSync('.'+action)) {
var img = fs.readFileSync('.'+action);
res.writeHead(200, {'Content-Type': 'image/gif' });
res.end(img, 'binary');
} else {

