some questions from a node.js newbie
i have just started learning node and i am using node-v0.4.11 on Fedora 12. It installed without any errors and i am able to run some basic programs but there are some things i cant figure out
If i create a server at a port and then if i stop node using Ctrl+C i cannot use that port again for starting a server without restarting the system.
my code looks like this
var port=8080;
var count=0;
var http=require('http');
var server = http开发者_运维知识库.createServer(function(req, res) {
count++;
console.log('request no '+count+' received');
res.writeHead(200);
res.end('you are at port '+port);
});
server.listen(port);
console.log('server started at port '+port);
Now when i save the file and execute it the server starts. But when i stop the server from the shell , i cannot start the server again at the port i started before(in this case port 8080).This is bad because every time i have to make changes in that file i have to change the port number too.Can i stop this behavior?
EDIT:
This is the error that i get when i try to start the server at the same port again
Server started at 8080
node.js:203
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: listen EADDRINUSE
at errnoException (net_uv.js:557:11)
at Array.<anonymous> (net_uv.js:632:28)
at EventEmitter._tickCallback (node.js:195:26)
i saw the process list after i terminated node
using Ctrl+C
and i found node was still running. So i killed it and checked the process list again to make sure it was dead and it was(i could not see it in the process list). Now i again tried to start the server at the same port but i got the same error as above.And once i stop the server and make request from the browser the request hangs up for ever(it keeps on waiting).
My next problem is that in the above when the server starts and i make the first request
count
is incremented 3 times and i can see 3 messages at the console. This is just for the first request,if i make more requestscount
is just incremented once. Whats wrong?this problem is solved
My next problem is that if i change
res.writeHead(200);
tores.writeHead(404);
or any other valid status code like300
node gives error at the command line but these are valid status codes and the server should start and respond to each request according the status code likeNot found
for404
. What am i doing wrong?this problem is solved
I am not able to use
Firebug
's 'Net' tab to monitor requests when i send requests to the server created by Node. Is there a way to do so?
Thanks for bearing.
i cannot start the server again at the port i started before
You should not be seeing this behavior, and I cannot reproduce it. Do you get an error message when you try to start the server again? What is it? Once you've stopped the server, do you see any node
processes in your process list? If so, can you start your server after you kill them?
count
is incremented 3 times and i can see 3 messages at the console
Your browser is sending multiple requests. For example, in Chrome, you might get (1) a pre-fetch request (where Chrome downloads the page it thinks you want), (2) the actual page request, and (3) a request for favicon.ico
; your Node app is listening for all requests, not just requests to the /
URL. After the initial request, the browser caches the favicon, etc. and doesn't need to request them again.
Try adding console.log(req.url);
to your callback and see what URLs the browser is asking for.
any other valid status code like
300
node gives error at the command line
I can't reproduce this problem. What error are you receiving?
I am not able to use Firebug's 'Net' tab to monitor requests when i send requests to the server created by Node
I'm also not able to reproduce this. Firebug and other client-side debugging tools don't know or care what technology is running on the server; it just speaks HTTP.
精彩评论