Updating the server code while running is not a simple and easy work.
You'd better to use a module such as https://github.com/learnboost/up .
To explain more detail of Ben's reply, server.close() only stops the
server
from accepting new connections and keeps existing connections.
See
http://nodejs.org/dist/v0.9.3/docs/api/net.html#net_server_close_callback
Additional keep-alived HTTP requests from a browser can be accpted via the
existing connection and they cause the error by multiple
server.close() calls.
If you want to close a http server with only one request from a brower ,
send 'Connection: close' header to client for ending the connection.
To be more safe, setting maxConnections = 1 is best to avoid concurrent
connections as below.
var http = require('http');
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain',
'Connection': 'close'});
res.end('Hello World\n');
server.close();
});
server.maxConnections = 1;
server.listen(8080, 0, function () {
console.log('Server running at http://localhost:8080/');
});
(2012/11/19 9:11), Ben Noordhuis wrote:
On Sun, Nov 18, 2012 at 7:02 PM, Érick Lavoie<[email protected]> wrote:
Hi,
I am trying to update the server code while it is running. If my
understanding is correct, I should first close the http server and once the
close operation has been performed, I can create another HttpServer
listening to the same port and host with the new code.
However, on Node 0.8.14, closing the server inside a request, like in the
following example:
var http = require('http');
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
server.close();
});
server.listen(8080, function () {
console.log('Server running athttp://localhost:8080/');
});
throws the following exception:
net.js:1046
throw new Error('Not running');
^
Error: Not running
at Server.close (net.js:1046:11)
at Server.<anonymous>
(/Users/erick/Documents/UdeM/Recherche/distributed-js/hello.js:6:12)
at Server.EventEmitter.emit (events.js:99:17)
at HTTPParser.parser.onIncoming (http.js:1807:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete]
(http.js:111:23)
at Socket.socket.ondata (http.js:1704:22)
at TCP.onread (net.js:403:27)
Surprisingly, closing the server in the listening callback does work. Is
there a way to do it while processing a request or is there some policy
preventing it from being done?
I wager you're testing it with a browser.
What happens is that you call server.close() twice because your
browser makes two requests, one for / and one for /favicon.ico. Make
the request with curl and you'll find that the server shuts down
gracefully.
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en