简体   繁体   English

Socket.IO/Node.JS执行

[英]Socket.IO/Node.JS execution

I'm new to both Node.JS and Socket.IO, so I decided to pick up a simple chat client as my first project. 我是Node.JS和Socket.IO的新手,所以我决定选择一个简单的聊天客户端作为我的第一个项目。 To my knowledge, it structured correctly, but I'm being thrown an object expected error whenever I try to run index.js . 据我所知,它的结构正确,但是每当我尝试运行index.js时,都会抛出object expected错误。 Anyone know what's up? 有人知道怎么回事吗? (if it helps, I'm running on Windows 8.1) (如果有帮助,我正在Windows 8.1上运行)

Code: 码:

Index.html Index.html

<!doctype html>
<html>
  <head>
    <title>TNjs Chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

index.js index.js

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

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

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

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

package.json package.json

{
  "name": "TNjsChat Client",
  "version": "0.0.1",
  "description": "A browser based chat system using Node.JS and socket.io, by Touka",
  "dependencies": {
    "express": "4.10.2",
    "socket.io": "1.2.0"
  }
}

You need to run node, not just let Microsoft IE run the js file which is what your screenshot seems to show. 您需要运行节点,而不仅仅是让Microsoft IE运行您的屏幕快照似乎显示的js文件。

Make sure node.exe is in the path somewhere and then run "node index.js" from the index.js directory and make sure all modules you need (like Express) are installed properly and available from the index.js directory. 确保node.exe在某个路径中,然后从index.js目录运行“ node index.js”,并确保正确安装了所需的所有模块(如Express),并且可以从index.js目录获得这些模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM