I want the following following node.js program to read a file and print out “Login failed.” if the text in the file is “Login failed.” however the “if (data == "Login failed.") {” line fails to return true and the program prints out “Login succeeded.” I’m using “CentOS release 5.5 (Final)” Linux distribution. Any idea as to what’s up? Thanks!
BASEPATH = '/home/wz3chen/node/';
fs.readFile(BASEPATH + 'node.js/' + "loginLog", function (err, data) {
console.log("DATA:" + data);
if (data == "Login failed.") {
console.log('Login failed.');
} else {
console.log('Login succeeded.');
}
});
One-line text files in Linux usually end in a new line.
It’s not clear whether you’re including
fs, so here is the code that works for me: