사용하여 파일 변경 config (설정) 할 때 웹 서버를 다시 시작 :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);
});
});
내가 설정 파일의 포트를 변경할 때, 나는 새 포트에 내 웹 서버에 액세스 할 수 있습니다 : 그래서 코드 아래에 추가합니다. 그러나 이전 포트에서도 액세스 할 수 있습니다. 나는 새로운 포트를 듣기 전에 이전 포트에서 웹 서버를 닫고 있다고 생각했습니다. 내가 뭘 놓치고 있니?
나는 당신의 도움 :)
내 2 센트, 설명 https://nodejs.org/api/net.html#net_server_close_callback으로,이 새로운 연결을 승인하지 않고 기존의 연결을 유지합니다. –
'keep-alive'는 서버가 연결을 닫지 않게하는 원인이된다고 생각합니다. –
대신 nodemon을 사용하지 마십시오 – UchihaItachi