I just created a node.js chat app.. but the app doesn’t load.
The code is very basic node.js chat app:
// 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");
After I compile it, I write in the chrome browser localhost:5000 but the page is keep loading and never finish.
However, the following code works perfectly:
// Load the TCP Library
net = require('net');
// Start a TCP Server
net.createServer(function (client) {
client.write("Hello World");
client.end();
}).listen(5000);
I run Windows 7 64 bit on my computer, and I’m using chrome.
Thanks in Advance!
You are creating a TCP/IP server by using the
netmodule, but you are accessing it using the http protocol using your web browser.This does not match each other.
Try to connect to your server using
telnet, e.g., and everything should be fine.Alternatively, if you want to be able to connect using your webbrowser, you need to use the
httpmodule instead of thenetmodule.