Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ENOENT, no such file or directory Node JS

I am trying to upoad and download images to the server via Node.js and I am using the below code:

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:

             ^
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??

like image 896
writeToBhuwan Avatar asked Sep 27 '14 00:09

writeToBhuwan


People also ask

How to fix error enoent no such file or directory?

92% additional asset processing scripts-webpack-plugin× 「wdm」: Error: ENOENT: no such file or directory, open....==> if anyone faced to such error, you should do followings: 1) you should check the if the file path is correct in angular.json file. 2) you should press crtl+c and re run the project. Show activity on this post.

Why do I get error when starting node server?

This error can appear while starting the node server. This can happen because of the no existence of some important files at the expected location. You can get this error if you use a file path that starts at the directory where scripts are executing but not at the project’s root.

What does the error “enoent” mean in Linux?

The “enoent” can appear because of some missing files or usage of the relative path that can be solved by creating an expected directory structure or using an absolute path, respectively. There are also many other reasons for getting this error.

How do I find the Rooth Directory of a node application?

Gives you the current node application's rooth directory. In your case, you'd use __dirname + '/Desktop/MyApp/newversion/partials/navigation.jade';


1 Answers

You need to check if the file exists with fs.existsSync(path) before attempting to read it:

 if (action !== '/' && fs.existsSync('.'+action)) {
    var img = fs.readFileSync('.'+action);
    res.writeHead(200, {'Content-Type': 'image/gif' });
    res.end(img, 'binary');
 } else {
like image 180
Dan D. Avatar answered Oct 20 '22 19:10

Dan D.