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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...