簡體   English   中英

避免手動重新加載頁面?

[英]Avoiding 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); 

試試這個: https//www.npmjs.com/package/reload

代碼更改時,在瀏覽器中自動刷新並重新加載代碼。 不需要瀏覽器插件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM