Node.js throw e error
I am m开发者_JAVA技巧essing around in Node.js trying to replicate that simple chat server that was floating around.
This is my JavaScript source file:
var net = require('net');
var carrier = require('carrier');
var connections = [];
var server = net.createServer(function(conn) {
connections.push(conn);
conn.on('close', function() {
var pos = connections.indexOf(conn);
if (pos >= 0) {
connections.splice(pos, 1);
}
});
conn.write("Hello, welcome to this chat server!\n");
conn.write("Please input your user name:\n");
var username;
carrier.carry(conn, function(line) {
if(!username) {
username = line;
conn.write("Hello " + username + "!\n");
return;
}
if(line == 'quit') {
conn.end();
return;
}
connections.forEach(function(one_connection) {
one_connection.write(line);
});
});
server.listen(8000);
As far as I can tell, everything in here is correct.
Now, when I try to run it through node.js, I get the following:
script.js:39
});
node.js:134
throw e;
There is some stuff that follows, but what I don't understand is node.js referencing line 39, which doesn't even exist. It's a 38-lined script with the last line being:
server.listen(8000);
So, what am I doing wrong?
I'm sorry if this is really simple, I'm very new at this, and it's kind of daunting.
Thanks!
You're missing a set of });
at the bottom. It's complaining about that.
From your indentation, it's from carrier.carry(conn, function(line) {
I strongly suggest getting an editor that can point out matching parens / braces (e.g. TextMate on a Mac or EditPlus on Windows)
精彩评论