简体   繁体   English

避免手动重新加载页面?

[英]Avoiding manually reloading the page?

It's working fine till logging into console about file change. 它正常工作,直到登录到控制台有关文件更改。 Is it possible to update the content of text file into the browser without manually reloading the page? 是否可以在不手动重新加载页面的情况下将文本文件的内容更新到浏览器中?

var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
    var q = url.parse(req.url, true);
    if(q.pathname == "/") {
    return res.end("Please request any file");      
    }
  if ( q.pathname == "/target.txt") {
    var filename = "C:\\Users\\Hp\\Documents\\Development" + q.pathname;    
  }
  else if(q.pathname == "/target1.txt") {
    var filename = "C:\\Users\\Hp\\Documents\\Development\\Node Programs\\file system module" + q.pathname;
  }
  else {
    return res.end("No such file found!");
  }
    fs.readFile(filename, function(err,data) {
    if(err) {
    return res.end("404 Not Found");
    } 
    res.writeHead(200,{'Content-Type': 'text/html'});
    res.write(data);
    res.write("Now watching "+ filename);
    console.log("Now watching file");
  });

  fs.watch(filename, () => {
    fs.readFile(filename, function(err,data) {
    res.writeHead(200,{'Content-Type': 'text/html'});
    res.write(data);
    res.write("Now watching "+ filename);    
    console.log("File changed");
    res.write(q.pathname+": File changed!");
    res.end();
  });
  });
  }).listen(8080); 

Try this: https://www.npmjs.com/package/reload 试试这个: https//www.npmjs.com/package/reload

Automatically refresh and reload your code in your browser when your code changes. 代码更改时,在浏览器中自动刷新并重新加载代码。 No browser plugins required. 不需要浏览器插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM