Fs.exists in node.js?

Good evening.
Started to read the book Node.js in action.
Here's the thing:
there is some code(to fully understand all of the code below):
var http = require('http'),
 fs = require('fs'),
 path = require('path'),
 mime = require('mime');

var cache = {};

function send404(res) {
Res. writeHead(404,{'Content-Type':'text/plain'});
 Res. write('Error 404: resourse not found');
Res. end();
}

function sendFile(res, filePath, fileContents) {
Res. writeHead(
200,
{'content-type':mime.lookup(path.basename(filePath))}
);
Res. end(fileContents);
}

function serveStatic(res, cache, absPath) {
 if(cache[absPath]) {
 sendFile(res, absPath, cache[absPath]);
 } else {
 fs.exists(absPath, function(exists) {
 if(exists) {
 fs.readFile(absPath, function(err, data) {
 if(err) {
send404(res);
 } else {
 cache[absPath] = data;
 sendFile(res, absPath, data);
}
});
 } else {
send404(res);
}
});
}
}

var server = http.createServer(function(req, res) {
 var filePath = false;

 if(req.url == '/') {
 filePath = 'public/index.html';
 } else {
 filePath = 'public' + req.url;
}

 var absPath = './' + filePath;
 server.Static(res, cache, absPath);
});

server.listen(3000, function() {
 console.log("Server listening on port 3000");
});


I am also interested in 1 thing - the serveStatic function.
On off. the website writes that the method fs.exists stability-0. And does not recommend to use it.
In the book code written in this way.

What I want to know:
-What is the fs method.exists and what is being passed in as the argument exists here :
fs.exists(absPath, function(exists) {
-More stable alternatives.
-And whether to use it at all?
July 2nd 19 at 13:53
1 answer
July 2nd 19 at 13:55
Solution
On off. the website writes that the method fs.exists stability-0. And does not recommend to use it.
...and answers, not strange to all of your questions. Well Doc it read well?

1. It checks for the existence of the file. The function is passed an argument of type boolean(true-false) which as you guess means if there's a file.
2. Doc says:
Use fs.stat() or fs.access() instead.
3. Depends on the version node.js.
Came across fs.stat. Replaced
fs.exists(absPath, function(exists) {
if(exists) {
fs.readFile(absPath, function(err, data) {
if(err) {
send404(res);
} else {
cache[absPath] = data;
sendFile(res, absPath, data);
}
});
} else {
send404(res);
}
});

This:

fs.stat(absPath, function(stat) {
if(stat) {
fs.readFile(absPath, function(err, data) {
if(err) {
send404(res);
} else {
cache[absPath] = data;
sendFile(res, absPath, data);
}
});
} else {
console.log('asd');
send404(res);
}
});

In the end, the browser displays Error 404: resourse not found.
The result was due to the fact that the file was not found.
and trail running code:
} else {
console.log('asd');
send404(res);
}

And in the first case, output the html page. - norene29 commented on July 2nd 19 at 13:58
What in the second embodiment prevents? - norene29 commented on July 2nd 19 at 14:01
All. I found a solution. Thank you so much for your time. - norene29 commented on July 2nd 19 at 14:04
in the same Doc says that is not necessary before reading to use stat, but simply to handle the error reading. - Kelsie.Purdy commented on July 2nd 19 at 14:07

Find more questions by tags Node.js