Jump to content
josh123

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

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.

 

 

Share this post


Link to post
Share on other sites

It's telling you that Node has successfully opened the file.

Share this post


Link to post
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

×