数据库更新后,nodejs自动刷新视图 [英] nodejs auto refresh view after database updates

查看:121
本文介绍了数据库更新后,nodejs自动刷新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次函数对数据库进行更改时,我想使用nodeJS刷新视图.如果我们以MEAN-stack为例,我不想每隔x秒发送$ http-request来检查是否对数据库进行了更改.我希望前端自动获得通知,然后更新视图.

I would like use nodeJS to refresh my view, every time a function has made changes to the database. If we take MEAN-stack as an example, I don't want to send an $http-request every x seconds to check if changes have been made to the database. I would like the front end to get notified automatically and then update the view.

什么是最佳做法?我会在服务器端使用某种Oberserver模式,但不知道该如何通知前端.

What are best practices for this? I would use some kind of Oberserver pattern in the server side, but do not know how I could notify the front end with that.

推荐答案

要使前端自动获得通知,然后更新视图,可以使用Socket.io框架.您可以在其网站上找到所有文档: http://socket.io/这是一个基本示例:

To get the front end to get notified automatically and then update the view you could use Socket.io framework. You can find all of the documentation on their site: http://socket.io/ And here is a basic example:

app.js(用于设置服务器)

app.js ( to set up the server)

var http = require('http');
var express = require('express');
var port = normalizePort(process.env.PORT || '1000');
var app = express();
var server = http.createServer(app);
server.listen(port);

io = require('socket.io')(server);

///ROUTES
var routes = require('./routes/index')(io);
var users = require('./routes/users');
///////

我将io对象传递给路由索引(当然,app.js上还有很多东西.这只是一个基本示例……)

I pass the io object to route index(and ofcourse there is a lot more stuff on app.js..this is just a basic example...).

mysql.js(创建用于连接的池)

mysql.js (to create a pool for connections)

var mysql = require("mysql");
var pool = mysql.createPool({
host     : 'host',
user     : 'user',
password : 'pass',
database : 'db_name',
connectionLimit: 1000
});

exports.pool = pool;

index.js

module.exports = function(io) {
 var express = require('express');
 var router = express.Router();
 var mysql = require('../mysql.js').pool;

 io.on('connection', function (socket) {
        socket.on('event_name', function (data) {
    mysql.getConnection(function(err,connection){
        if (err) {
          connection.release();
          return;
        }               
         connection.query("SQL STUFF",function(err,rows){
            if(rows.length>0){//checks if there are more than 0 rows returned.....
                socket.emit('do_something',data_you_want_to_pass);
            }
            else{
                socket.emit('do_something_else',data_you_want_to_pass);
            }
            connection.release();    
         });

          connection.on('error', function(err) {      
            return;    
          });
    });
  });

});

router.get('/', function(req, res) {
res.render("index"); 
});

return router;
}

然后在html页面上,再次有socket.emit和socket.on .....

And then on html page you have socket.emit and socket.on again.....

我建议您看一下文档和其他一些示例...

I recommend you take a look at the documentation and a few other examples...

希望我能对您有所帮助.

I hope I helped you.

这篇关于数据库更新后,nodejs自动刷新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆