node.js - Can not set headers error - on file content change -


i new node.js , having issues while coding it

i using node.js , express

following code :

var express = require('express'); var app = express(); var fs = require('fs');  var filepath = 'answer2.txt'; var file = fs.readfilesync(filepath);  app.get('/', function (req, res) {     fs.watch(filepath, function(event, filename) {   if(filename){        file = fs.readfilesync(filepath);     //console.log('\n' + file);     return res.send('\n' + file);    }   else{     console.log('filename not provided')   } });    });  var server = app.listen(8000, function () {    console.log("example app listening"); }); 

purpose of code : content of file answer2.txt changes , saved, should reflect new content on webpage without refreshing page. functionality working console browser, throws following error :

_http_outgoing.js:357     throw new error('can\'t set headers after sent.');     ^  error: can't set headers after sent.     @ serverresponse.outgoingmessage.setheader (_http_outgoing.js:357:11)     @ serverresponse.header (c:\xampp\htdocs\rest\node_modules\express\lib\response.js:730:10)     @ serverresponse.send (c:\xampp\htdocs\rest\node_modules\express\lib\response.js:170:12)     @ fswatcher.<anonymous> (c:\xampp\htdocs\rest\expresso.js:14:13)     @ emittwo (events.js:106:13)     @ fswatcher.emit (events.js:191:7)     @ fsevent.fswatcher._handle.onchange (fs.js:1426:12) 


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -