Javascript 文件内容更改时自动重新加载浏览器,保存在nodejs中

Javascript 文件内容更改时自动重新加载浏览器,保存在nodejs中,javascript,node.js,express,Javascript,Node.js,Express,我一直在从事node.js项目。我的要求是在浏览器上加载.txt文件。当我更改并保存此文件时,应更新内容。浏览器应该是自动刷新的 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); index.js app.get('/', function(req, res){ res.sen

我一直在从事
node.js
项目。我的要求是在浏览器上加载.txt文件。当我更改并保存此文件时,应更新内容。浏览器应该是自动刷新的

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);

    index.js
    app.get('/', function(req, res){
      res.sendFile(__dirname + '/demo.txt');
    });

    io.on('connection', function(socket){
      console.log('a user connected');
    });

    http.listen(3000, function(){
      console.log('listening on *:3000');
    });

    var io = require('socket.io')(80);
    var fs = require('fs');

    fs.watchFile('message.text', (curr, prev) => {
      console.log(`the current mtime is: ${curr.mtime}`);
      console.log(`the previous mtime was: ${prev.mtime}`);
      // file changed push this info to client.
      io.emit('fileChanged', 'yea file has been changed.');
    });

    index.html
    <script>
      var socket = io();
    socket.on('fileChanged', function(msg){
      alert(msg);
    });
var-app=require('express')();
var http=require('http')。服务器(应用程序);
var io=require('socket.io')(http);
index.js
app.get('/',函数(req,res){
res.sendFile(uu dirname+'/demo.txt');
});
io.on('连接',函数(套接字){
log(“已连接的用户”);
});
http.listen(3000,函数(){
console.log('监听*:3000');
});
var io=require('socket.io')(80);
var fs=需要('fs');
fs.watchFile('message.text',(curr,prev)=>{
log(`当前mtime为:${curr.mtime}`);
log(`上一个mtime是:${prev.mtime}`);
//文件更改将此信息推送到客户端。
io.emit('fileChanged','yea文件已更改');
});
index.html
var socket=io();
socket.on('fileChanged',函数(msg){
警报(msg);
});

使用Ajax对文件进行轮询。您的服务器可以响应
{changes:{timestamp of file modify}}
。现在检查您最后看到的更改时间是否与响应时间不同


如果有更改:
window.location.reload

执行此操作的最佳方法是使用WebSocket。使用WebSockets的一个非常好的软件包是,您可以使用诸如或本机函数之类的东西来监视文件更改,然后发出消息


或者,如果您仅为开发目的而尝试执行此操作,则应选中或其他具有内置函数的任务运行程序来执行此操作。

首先,您可以通过两个操作来执行此操作:

1。监视服务器端的文件更改。并将信息推送到客户端

您可以使用node.js查看文件

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
    res.sendFile(__dirname + '/cluster.json');
});

const io = require('socket.io')(http);
io.on('connection',function (client) {
    console.log("Socket connection is ON!");
});

http.listen(80, function(){
    console.log('listening on *:80');
});
var fs = require('fs');

fs.watchFile('cluster.json', function(curr, prev){
    // file changed push this info to client.
    console.log("file Changed");
    io.emit('fileChanged', 'yea file has been changed.');
});
2。在客户端捕获“文件更改”信息并刷新页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
    <script type="text/javascript" src="node_modules/socket.io-client/dist/socket.io.js"></script>
    <script>
    var socket = io("http://localhost:80");
    socket.on('fileChanged', function(msg){
        alert(msg);
    });
    </script>
</body>
</html>

标题
变量套接字=io(“http://localhost:80");
socket.on('fileChanged',函数(msg){
警报(msg);
});

如何保存更改?是否要在浏览器中编辑文件并在
x
发生时将其发送到服务器?.txt格式,我使用上述代码加载文件,如何自动刷新浏览器。保存更改后,.txt文件中的内容应显示在浏览器上。您无法在服务器端“自动刷新”浏览器服务器无法连接到客户端并推送数据。您必须从浏览器设置websocket连接,服务器可以使用该连接向浏览器发送消息,告知在浏览器中运行的脚本刷新页面,或者,在浏览器中设置一个脚本,定期询问服务器文件是否已更新,如果您说节点ajax设置错误io不起作用,则刷新页面。我再次发布了代码,是否正确?您需要在html中包含socket.io客户端,如@hurricane wrote您的意思是index.html?如果它被称为index。html@Ramsocket.io由服务器和客户端两部分组成。第一个代码必须在服务器端(node.js),第二个代码必须在客户端(index.html)