SyntaxError:意外的令牌<

时间:2015-09-27 01:22:47

标签: node.js socket.io

我正在尝试使用NodeJs和Socket.io制作我的第一个推送通知脚本 我找到了这个教程: http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/

这真的很有帮助,但我坚持了几个小时,不能再往前走。

我创建了server.js和client.html文件。当我打电话给节点服务器时它正在工作,当有人进入时会更新控制台等。但是当我在浏览器上访问我的网站时,我没有看到我的数据库中的任何内容。

当我尝试在控制台(node client.html)上运行client.html时,我收到以下错误:

root@Node:~# node client.html
/root/client.html:1 (function (exports, require, module, __filename, __dirname) { <html>
                                                                                  ^

SyntaxError: Unexpected token <
     at exports.runInThisContext (vm.js:53:16)
     at Module._compile (module.js:413:25)
     at Object.Module._extensions..js (module.js:452:10)
     at Module.load (module.js:355:32)
     at Function.Module._load (module.js:310:12)
     at Function.Module.runMain (module.js:475:10)
     at startup (node.js:117:18)
     at node.js:951:3

client.html:

$test

server.js:

<html>
<head>
<title>Test</title>
</head>
    <body>
        <time></time>
        <div id="container">Loading ...</div>
    <script src="socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>

        // create a new websocket
        var socket = io.connect('http://46.101.226.135:8000/');
        // on message received we print all the data inside the #container div
        socket.on('notification', function (data) {
        var usersList = "<dl>";
        $.each(data.test_payout,function(index,user){
            usersList += "<dt>" + user.nick + "</dt>\n" +
                         "<dd>" + user.payout + "\n" +
                            "<figure> <img class='img-polaroid' width='50px' src='" + user.country + "' /></figure>"
                         "</dd>";
        });
        usersList += "</dl>";
        $('#container').html(usersList);

        $('time').html('Last Update:' + data.time);
      });
    </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

您只需要运行

node server.js

确保您通过npm安装 socket.io mysql

然后将浏览器指向http://localhost:8000以从节点服务器获取client.html。

似乎数据库凭据不再有效,因为命令行中的输出表明。

{ [Error: Connection lost: The server closed the connection.] fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
相关问题