Node.js聊天应用程序不';t载荷

Node.js聊天应用程序不';t载荷,node.js,Node.js,我刚刚创建了一个node.js聊天应用程序。。但应用程序无法加载。 代码是非常基本的node.js聊天应用程序: // Load the TCP Library var net = require('net'); // Keep track of the chat clients var clients = []; // Start a TCP Server net.createServer(function (client) { console.log("Connected!")

我刚刚创建了一个node.js聊天应用程序。。但应用程序无法加载。 代码是非常基本的node.js聊天应用程序:

// Load the TCP Library
var net = require('net');

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (client) {

    console.log("Connected!");

    clients.push(client);

    client.on('data', function (data) {
        clients.forEach(function (client) {
            client.write(data);
        });
    });

}).listen(5000);

// Put a friendly message on the terminal of the server.
console.log("Chat server is running\n");
编译后,我在chrome浏览器
localhost:5000中编写代码,但页面一直在加载,从未完成

但是,以下代码可以完美地工作:

// Load the TCP Library
net = require('net');

// Start a TCP Server
net.createServer(function (client) {
    client.write("Hello World");
    client.end();
}).listen(5000);

我在我的电脑上运行64位Windows7,我使用的是chrome。


提前谢谢

您正在使用
net
模块创建TCP/IP服务器,但您正在使用web浏览器使用http协议访问它

这两者不匹配

尝试使用
telnet
连接到您的服务器,例如,一切正常


或者,如果您希望能够使用webbrowser进行连接,则需要使用
http
模块而不是
net
模块。

您正在使用
net
模块创建TCP/IP服务器,但您正在使用web浏览器使用http协议访问它

这两者不匹配

尝试使用
telnet
连接到您的服务器,例如,一切正常


或者,如果您希望能够使用webbrowser进行连接,则需要使用
http
模块,而不是
net
模块。

网络库(如果用于TCP,而不是http)。如果您使用TCS,您应该能够通过telnet而不是浏览器访问聊天室

这是一个关于如何为HTTP编写一个的示例(从)


网络库(如果适用于TCP,则不适用于HTTP)。如果您使用TCS,您应该能够通过telnet而不是浏览器访问聊天室

这是一个关于如何为HTTP编写一个的示例(从)


打开tcp连接时不要使用浏览器进行测试

简单测试
telnet localhost 5000
在您的控制台中。

打开tcp连接时不要使用浏览器进行测试

简单测试
telnet localhost 5000
在控制台中。

是否在控制台中打印
“聊天服务正在运行”
?是否在控制台中打印
“聊天服务正在运行”
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');