title: How to exit function in Node.js
body: <p>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 <code>break;</code> 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.</p>
  
  <p><strong>How should I be causing it to exit the function?</strong></p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>I am using the following code from <a href="http://hectorcorrea.com/blog/introduction-to-node-js/51" rel="nofollow noreferrer">this site</a> with Node.js:</p>
  
  <pre><code>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 = "&lt;p&gt;Hello World, the time is " + now + "- Messages: " + message + ".&lt;/p&gt;";
      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);
  });
  </code></pre>