Jump to content

Node.js Read Stream-- check if file is open-- file is "always" open?


josh123

Recommended Posts

I copied the code straight from 

https://www.w3schools.com/nodejs/shownodejs_cmd.asp?filename=demo_events_open

open.js

var fs = require('fs');

var readStream = fs.createReadStream('./demofile.txt');

/*Write to the console when the file is opened:*/
readStream.on('open', function () {
  console.log('The file is open');
});

 

Upon executing "node open.js" in the terminal, "The file is open" appears in the terminal, then the program terminates. It always happens, even if I don't have demofile.txt open in Notepad or any other program. Not sure why this is happening?

When I instead use the string 'close', the console never prints anything out, regardless as to demofile.txt is open or closed.

This behavior has happened on both my Windows machine and Raspberry Pi.

Tried putting the above code inside a http.createServer statement, but then I get no output at all in the console. Although I suppose that would be unnecessary.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...