Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将DB节点更改提要转换为HTML_Node.js_Real Time_Rethinkdb - Fatal编程技术网

Node.js 将DB节点更改提要转换为HTML

Node.js 将DB节点更改提要转换为HTML,node.js,real-time,rethinkdb,Node.js,Real Time,Rethinkdb,我试图显示从数据库到HTML(前端)的任何更改提要,但无法显示任何更改提要 你能帮我解决这个问题吗 文件是app.js var express = require('express'), app = express(), server = require('http').createServer(app), users = {}, db='testing', table = 'todos'; io = require('socket.io').li

我试图显示从数据库到HTML(前端)的任何更改提要,但无法显示任何更改提要

你能帮我解决这个问题吗

文件是app.js

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    users = {},
    db='testing',
    table = 'todos';
    io = require('socket.io').listen(server);
var bodyParser = require('body-parser');

server.listen(5000);

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

var config = require(__dirname + '/config.js');
var r = require('rethinkdbdash')(config.rethinkdb);

io.sockets.on('connection', function(socket){
    r.db(db).table(table).pluck('title').changes().run().
        then(function(feed){
            feed.each(function(err, item){
                console.log(JSON.stringify(item, null, 2));
                io.emit('new message', item);   
            }); 
    });
});
config.js文件

module.exports = {
  rethinkdb: {
    host: '192.168.2.3',
    port: 28015,
    authKey: '',
    db: 'testing'
  },
  express: {
     port: 5000
  }
};
<html>
<head>
    <title>Chat with socket.io and node.js</title>
    <style>
        #chat{
            height:500px;
        }
    </style>
</head>
<body>
    <div id="chat"></div>
    <form id="send-message">
        <input size="35" id="message"></input>
        <input type="submit"></input>
    </form>

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        jQuery(function($){
            var socket = io.connect();
            var $messageForm = $('#send-message');
            var $messageBox = $('#message');
            var $chat = $('#chat');

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit('send message', $messageBox.val());
                $messageBox.val('');
            });

            socket.on('new message', function(data){
                $chat.append(data + "<br/>");


        });
    </script>
</body>
</html>
index.html文件

module.exports = {
  rethinkdb: {
    host: '192.168.2.3',
    port: 28015,
    authKey: '',
    db: 'testing'
  },
  express: {
     port: 5000
  }
};
<html>
<head>
    <title>Chat with socket.io and node.js</title>
    <style>
        #chat{
            height:500px;
        }
    </style>
</head>
<body>
    <div id="chat"></div>
    <form id="send-message">
        <input size="35" id="message"></input>
        <input type="submit"></input>
    </form>

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        jQuery(function($){
            var socket = io.connect();
            var $messageForm = $('#send-message');
            var $messageBox = $('#message');
            var $chat = $('#chat');

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit('send message', $messageBox.val());
                $messageBox.val('');
            });

            socket.on('new message', function(data){
                $chat.append(data + "<br/>");


        });
    </script>
</body>
</html>

与socket.io和node.js聊天
#聊天{
高度:500px;
}
jQuery(函数($){
var socket=io.connect();
var$messageForm=$(“#发送消息”);
var$messageBox=$(“#message”);
var$chat=$(“#chat”);
$messageForm.submit(函数(e){
e、 预防默认值();
emit('send message',$messageBox.val());
$messageBox.val(“”);
});
socket.on('new message',函数(数据){
$chat.append(数据+“
”); });
index.html上出现了一些语法错误,导致客户端无法运行,那么主要问题是您选取了错误的字段

以下是有效的代码:

app.js

var express = require('express'),
app = express(),
server = require('http').createServer(app),
users = {},
db='testing',
table = 'todos';
io = require('socket.io').listen(server);
var bodyParser = require('body-parser');

server.listen(5000);

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

var config = require(__dirname + '/config.js');
var r = require('rethinkdbdash')(config.rethinkdb);

io.sockets.on('connection', function(socket){
  r.db(db).table(table)
  .pluck('title')
  .changes()
  .run()
  .then(function(feed){
    feed.each(function(err, item){
      console.log(JSON.stringify(item, null, 2));
      io.emit('new message', item.new_val.title);
    })
  })
})
index.html

<html>
<head>
  <title>Chat with socket.io and node.js</title>
  <style>
#chat{
  height:500px;
}
  </style>
</head>
<body>
  <div id="chat"></div>
  <form id="send-message">
    <input size="35" id="message"></input>
    <input type="submit"></input>
  </form>

  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script src="/socket.io/socket.io.js"></script>
  <script>
jQuery(function($){
  var socket = io.connect();
  var $messageForm = $('#send-message');
  var $messageBox = $('#message');
  var $chat = $('#chat');

  $messageForm.submit(function(e){
    e.preventDefault();
    socket.emit('send message', $messageBox.val());
    $messageBox.val('');
  });

  socket.on('new message', function(data){
    $chat.append(data + "<br/>");


  });
})
  </script>
</body>
</html>
如果返回整个文档,则客户端
$chat.append(data+“
”)上的字符串concat
可能会收到错误或“object object”字符串,因为数据是对象,而不是字符串

如果仅返回如下标题:

{
  "new_val": {
    "id": "862e6410-f686-4a70-8a52-d4387181d4f2",
    "title": "12"
  },
  "old_val": null
}
io.emit('new message', item.new_val.title);
然后字符串concat将正常运行


<>也,你的代码很乱。我建议你在一般情况下得到JavaScript的一些基本基础。谢谢你的回答。你如何将更改进给转换成一个数组?这是一个JSON对象数组。