1

I want the program to stop listening for requests and continue on with the code after it has successfully loaded the page once. I have tried putting a break; at the end of the code in the function, but it says that is an illegal break. I don't know what I should be using instead of break.

How should I be causing it to exit the function?

Thanks in advance.


I am using the following code from this site with 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);
});
Ryan
  • 105
  • 4
  • 14

2 Answers2

2

You're not exiting a function (which would be return, not break), you're telling the object that is listening on a port to stop doing so.

If you want the server to stop listening for new connections, use its 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);

Note that the server won't close immediately. http.Server#close calls net.Server.close, which says:

Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event. The optional callback will be called once the 'close' event occurs. Unlike that event, it will be called with an Error as its only argument if the server was not open when it was closed.

In my quick local test, that takes a while. You can make it more proactive if you intercept the connection via the 'connection' event as described in this answer, and destroy the connection as well as closing the server:

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);
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I updated the question. I put the wrong code first time (sorry). The server.close(); doesn't work how I want it to. It gets stuck at "b" – Ryan Apr 06 '17 at 15:47
  • @Ryan: If you want to do something else once you've closed the server, you need to include code to do that. For instance, perhaps calling your `dataHandler` function, or wrapping your `consumer` stuff in a function and calling that. You haven't said **what** you want to happen next, so we can't help you precisely, but that's what you'd do. I've noted above where you'd do that. – T.J. Crowder Apr 06 '17 at 15:50
  • I want it to continue getting data from Kafka – Ryan Apr 06 '17 at 16:04
  • @Ryan: Great -- do that. Nothing's stopping you. Just put the necessary code in a function, and call it where indicated. – T.J. Crowder Apr 06 '17 at 16:18
0

You can stop the server from listening by calling server.close().

Marc
  • 1,900
  • 14
  • 22