Node.js fs.watch - outputs multiple times for sing

2019-07-18 03:52发布

问题:

I am creating a local app that uses node.js on windows. the idea is to watch a specific file and send the contents to the browser when the file changes.

right now, for testing, I am just looking at the fs.watch on the file from the console. What I have noticed is that a single change to the file I am watching creates double output to the console.

here is the server:

var fs, http, io, server;
fs = require('fs');
http = require('http');
server = http.createServer(function(req, res) {
  return fs.readFile("" + __dirname + "/socket.io.demo.html", function(err, data) {
    res.writeHead(200, {
      'Content-Type': 'text/html'
    });
    return res.end(data, 'utf8');
  });
});
server.listen(1337);

fs.watch('/', function (event, filename) {
  console.log('event is: ' + event);
  if ('test.txt') {
    console.log('filename provided: ' + filename);
  } else {
    console.log('filename not provided');
  }
});

io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket) {
  socket.on('publish', function(message) {
    return io.sockets.send(message);
  });
  socket.on('broadcast', function(message) {
    return socket.broadcast.send(message);
  });
  return socket.on('whisper', function(message) {
    return socket.broadcast.emit('secret', message);
  });
});

here is the html:

<!doctype html>
<html>
  <head>
    <title>Socket.IO demo</title>
  </head>
  <body>
    <h1>Socket.IO demo</h1>
    <input type="text" autofocus="autofocus" />
    <button type="button">publish</button>
    <button type="button">broadcast</button>
    <button type="button">whisper</button>
    <p>Status: <span id="status">Undefined</span></p>
    <ol id="messages"></ol>
    <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
    <script type="text/coffeescript">

      jQuery ($) ->

        $status = $ '#status'
        socket = io.connect()

        socket.on 'connect', ->
          $status.text 'Connected'

        socket.on 'disconnect', ->
          $status.text 'Disconnected'

        socket.on 'reconnecting', (seconds) ->
          $status.text "Reconnecting in #{seconds} seconds"

        socket.on 'reconnect', ->
          $status.text 'Reconnected'

        socket.on 'reconnect_failed', ->
          $status.text 'Failed to reconnect'

        socket.on 'message', (message) ->
          $('<li>').text(message).appendTo $('#messages')

        socket.on 'secret', (message) ->
          console.log message

        $input = $ 'input'

        $('button').click ->
          socket.emit $(this).text(), $input.val()
          $input.val('').focus()

    </script>
  </body>
</html>

Any thoughts on why fs.watch is creating double output in the console?

回答1:

Looks like a Node bug. https://github.com/joyent/node/issues/2126

As a temporary solution you could consider using setTimeout to smooth out the events.