2016-12-16 14 views
0

を使用してファイルの変更をconfigの場合は、以下のように私はNodeJSで書かれたシンプルで働くWebサーバーを持っている:今、私は私のウェブサーバが再起動したときに、ポート番号の新しいポートを聞きたい再起動、WebサーバーNodeJS

var http = require("http"); 
var fs = require("fs"); 

console.log("Web server started"); 
var config = JSON.parse(fs.readFileSync("./private/config.json")); 

var server = http.createServer(function(req,res){ 

    console.log("received request: " + req.url); 
    fs.readFile("./public" + req.url,function(error,data){ 

     if (error){ 

      // Not sure if this is a correct way to set the default page? 
      if (req.url === "/"){ 
       res.writeHead(200,{"content-type":"text/plain"}); 
       res.end("here goes index.html ?"); 
      } 

      res.writeHead(404,{"content-type":"text/plain"}); 
      res.end(`Sorry the page was not found.\n URL Request: ${req.url}`); 
     } else { 
      res.writeHead(200,{"content-type":"text/plain"}); 
      res.end(data); 
     } 

    }); 
}); 

設定ファイルの変更。だから私は、コードの下に追加します。

fs.watch("./private/config.json",function(){ 
    config = JSON.parse(fs.readFileSync("./private/config.json")) 
    server.close(); 
    server.listen(config.port,config.host,function(){ 
     console.log("Now listening: "+config.host+ ":" +config.port); 
    }); 
}); 

これは正常に動作し、私は、configファイルのポートを変更したときに、私は新しいポート上で私のWebサーバにアクセスすることができます。ただし、以前のポートでもアクセスできます。新しいポートを聞く前に、前のポートでWebサーバーを閉じていると思っていました。私は何が欠けていますか?

私はあなたの助けに感謝:)

+0

私の2セント、説明https://nodejs.org/api/net.html#net_server_close_callbackとして、それは新しい接続を受け付けを停止し、既存の接続を維持します。 –

+0

私は、 'keep-alive'がサーバに接続を閉じないようにさせていると思います。 –

+0

なぜノーデモンを使用しないのですか – UchihaItachi

答えて

0

ムケシュ・シャルマは述べたように、Server.closeは()受け入れて新しい接続を停止し、既存の接続を維持します。つまり、サーバはすべてのアライブソケットに対してオープンされたままです(キープアライブ時間のために自然に消えるまで)が、新しいソケットは作成されません。

私はこの質問がthis question

の可能複製できるように、私はリンクにゴロローデンで述べた提案された解決策を踏襲し、それが働いてました。基本的には、オープンソケット接続を覚えておいて、サーバを閉じた後でそれらを破壊する必要があります。ここに私の修正コードです:

var http = require("http"); 
var fs = require("fs"); 

console.log("Web server started"); 
var config = JSON.parse(fs.readFileSync("./private/config.json")); 

var server = http.createServer(function(req,res){ 

    console.log("received request: " + req.url); 
    fs.readFile("./public" + req.url,function(error,data){ 

     if (error){ 

      // Not sure if this the correct method ? 
      if (req.url === "/"){ 
       res.writeHead(200,{"content-type":"text/plain"}); 
       res.end("welcome to main page"); 
      } 

      res.writeHead(404,{"content-type":"text/plain"}); 
      res.end(`Sorry the page was not found.\n URL Request: ${req.url}`); 
     } else { 
      res.writeHead(200,{"content-type":"text/plain"}); 
      res.end(data); 
     } 

    }); 
}); 

server.listen(config.port,config.host,function(){ 
    console.log("listening: "+config.host+ ":" +config.port); 
}); 

var sockets = {}, nextSocketId = 0; 
server.on('connection', function (socket) { 

    // Add a newly connected socket 
    var socketId = nextSocketId++; 
    sockets[socketId] = socket; 
    console.log('socket', socketId, 'opened'); 

    // Remove the socket when it closes 
    socket.on('close', function() { 
    console.log('socket', socketId, 'closed'); 
    delete sockets[socketId]; 
    }); 

}); 

fs.watch("./private/config.json",function(){ 
    config = JSON.parse(fs.readFileSync("./private/config.json")) 
    console.log('Config has changed!'); 
    server.close(function() { console.log('Server is closing!'); }); 

    for (var socketId in sockets) { 
     console.log('socket', socketId, 'destroyed'); 
     sockets[socketId].destroy(); 
    } 

    server.listen(config.port,config.host,function(){ 
    console.log("Now listening: "+config.host+ ":" +config.port); 
    }); 
}); 
+0

今私は別の質問があります。私は、ソケットforループをserver.close()のコールバック関数に入れようとしましたが、動作しませんでした。エラーは発生しませんが、動作しません。何故ですか ? –

関連する問題