title: Setting up kafka listener in nodejs microservice
body: <p>I am wondering about how to proceed with the set up of kafka listener. Should I be running two separate processes <code>node server.js</code> and <code>node kafka-listener.js</code> for every micro service?</p>
  
  <p>I have some sample code of both of them:</p>
  
  <p>This is <code>server.js</code> which runs the node server</p>
  
  <pre><code>    require('dotenv').config() // Accesses the environment variables from .env file at the root of the folder or from the docker environment
  
  var port = process.env.PORT;
  const express = require("express"),
      createtables = require("./config/createtables.js"),
      vogels = require('vogels'),
      morgan = require('morgan'),
      logger = require('./config/logger'),
      app = express(),
      bodyParser = require("body-parser");
  
  createtables();
  
  
  app.use(bodyParser.urlencoded({extended:true}));
  app.use(bodyParser.json());
  
  //We are using a different port for testing because we might want to have the development server ON when we run tests
  if (process.env.NODE_ENV === "test") {
      port = 3002
  }
  
  app.use(morgan('dev', {
      skip: function (req, res) {
          return res.statusCode &lt; 400
      }, stream: process.stderr
  }));
  
  app.use(morgan('dev', {
      skip: function (req, res) {
          return res.statusCode &gt;= 400
      }, stream: process.stdout
  }));
  
  var routes = require('./api/routes/LRRoutes');
  routes(app);
  
  
  app.listen(port, function(){
      logger.info('Example app listening on port ' + port);
  });
  
  module.exports = app; // for testing
  </code></pre>
  
  <p>This is a sample kafka listener (<code>kafka-listener.js</code>):</p>
  
  <pre><code>var kafka = require('kafka-node'),
          Consumer = kafka.Consumer,
          client = new kafka.Client(),
          consumer = new Consumer(client,
                      [{ topic: 'name', offset: 0}],
                      {
                                      autoCommit: false
                                  }
                  );
  
  consumer.on('message', function (message) {
          console.log(message);
  });
  
  consumer.on('error', function (err) {
          console.log('Error:',err);
  })
  
  consumer.on('offsetOutOfRange', function (err) {
          console.log('offsetOutOfRange:',err);
  })
  </code></pre>
  
  <p>Should the listener be run as a separate process? Or is there any other alternative to this? </p>
  
  <p>If you have inputs how routing is done once a there is a message to the listener. Please add that too. </p>