2017-04-06 130 views
0

我希望程序停止監聽請求,並在代碼成功加載一次頁面後繼續執行代碼。我曾嘗試在代碼的末尾放置一個break;,但它表示這是非法中斷。我不知道我應該用什麼來代替休息。如何在Node.js中退出函數

我該如何導致它退出該功能?

在此先感謝。


我使用從this site下面的代碼使用的是Node.js:

var http = require("http"); 
var port = 3000; 
var serverUrl = "localhost"; 

var Kafka = require('no-kafka'); 
var connString = ' kafka://192.168.0.108:9092, 192.168.0.108:9092 ' 
var consumer = new Kafka.SimpleConsumer({ connectionString: connString }); 

var message = ["start"]; 

var server = http.createServer(function(req, res) { 
    console.log("Request: " + req.url); 
    var now = new Date(); 
    var html = "<p>Hello World, the time is " + now + "- Messages: " + message + ".</p>"; 
    res.end(html); 
    console.log("a"); 
    server.close(); 
    console.log("b"); 
}); 

// data handler function can return a Promise 
var dataHandler = function (messageSet, topic, partition) { 
    messageSet.forEach(function (m) { 
     console.log('topic received: '); 
     console.log({ 
      'topic':topic, 
      'partition': partition, 
      'offset': m.offset, 
      'message': m.message.value.toString('utf8')  
     }); 
     message.push(m.message.value.toString('utf8')); 

     console.log("Listening at " + serverUrl + ":" + port); 
     server.listen(port, serverUrl); 

     console.log("c"); 
    }); 
}; 

return consumer.init() 
.then(function() { 
    return consumer.subscribe('temp', 0, dataHandler); 
}); 
+0

server.close(); –

+2

我會考慮重新提出問題。 – Orangesandlemons

+0

server.close();不起作用。我更新了正確的代碼問題。它卡在「b」處,並且只會在控制檯中按ctrl-c時繼續。 – Ryan

回答

2

你不是退出功能(這將是return,不break),你告訴在端口上監聽的對象停止這樣做。

如果您希望服務器停止監聽新連接,請使用其close method

var http = require("http"); 
var port = 3000; 
var serverUrl = "localhost"; 

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

    console.log("Request: " + req.url); 

    var now = new Date(); 
    var html = "<p>Hello World, the time is " + now + ".</p>"; 
    res.end(html); 
    server.close(); // <======================================= 
    // *** If you want to do something else at this point, call it from here *** 
}); 

console.log("Listening at " + serverUrl + ":" + port); 
server.listen(port, serverUrl); 

請注意,服務器不會立即關閉。 http.Server#close來電net.Server.close,其中說:

停止服務器接受新的連接,並保持現有的連接。此功能是異步的,當所有連接結束並且服務器發出事件時,服務器最終關閉。一旦發生'close'事件,將會調用可選callback。與該事件不同,如果服務器在關閉時未打開,它將以Error作爲唯一參數進行調用。

在我的快速本地測試中,這需要一段時間。你可以把它更加積極主動,如果你攔截通過'connection'事件as described in this answer連接,並摧毀連接以及關閉服務器:

var http = require("http"); 
var port = 3000; 
var serverUrl = "localhost"; 

var connections = []; 
var server = http.createServer(function(req, res) { 

    console.log("Request: " + req.url); 

    var now = new Date(); 
    var html = "<p>Hello World, the time is " + now + ".</p>"; 
    res.end(html); 
    connections.forEach(function(con) { // *** 
     con.destroy();     // *** 
    });         // *** 
    server.close();      // *** 
    // *** If you want to do something else at this point, call it from here *** 
}); 
server.on("connection", function(con) { // *** 
    connections.push(con);    // *** 
});          // *** 

console.log("Listening at " + serverUrl + ":" + port); 
server.listen(port, serverUrl); 
+0

我更新了問題。我第一次把錯誤的代碼(抱歉)。 server.close();不符合我的要求。它被卡在「b」 – Ryan

+0

@Ryan:如果你想在關閉服務器之後做其他事情,那麼你需要包含代碼來做到這一點。例如,可能調用你的'dataHandler'函數,或者將你的'consumer'東西包裝到一個函數中並調用它。你沒有說**你想要發生什麼**,所以我們無法準確幫助你,但這就是你要做的。我已經在上面指出了你會這樣做的地方。 –

+0

我希望它可以繼續從卡夫卡獲取數據 – Ryan

0

您可以通過調用server.close()聽停止服務器。