Using node bin/www with twits I use the following code which is working as expected, but I want to use it in production. I'm not sure that this is clean code since I'm new to node.js. It would be great if I can get some example how can I write cleaner code.


I mean how do I use the config to read the values, there is better way?
How do I use the onListening?




/**
 * Module dependencies.
 */
var app = require('../app');
var debug = require('debug')('blog-twitter:server');
var http = require('http');
var os=require('os');
var request=require('superagent')
/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '8062');
app.set('port', port);


/**
 * Create HTTP server.
 */
//var io=require('express-socket.io');

var server = http.createServer(app);
var io=require('socket.io').listen(server);
var Twitter=require('twitter');
var cfg = require('../config.json');
//console.log(cfg.twitter);
/**
*Create a twitter client to use AUTH credentials defined
*in config.json file
*/
var client=new Twitter({
  consumer_key: cfg.twitter.consumer_key,
  consumer_secret: cfg.twitter.consumer_secret,
  access_token_key: cfg.twitter.access_token_key,
  access_token_secret: cfg.twitter.access_token_secret
});
/**
Bind to twitter public streaming API
*/
var stream=client.stream('statuses/filter',{track:"Javascript"});
/**
*Create twitter stream, when a tweet is available, 
*get its embeded format and emit that to the server.
*/
stream.on("data", function(data){
  console.log(data.id_str);
  var tweet_id="https://api.twitter.com/1.1/statuses/oembed.json?id="+data.id_str;
  //console.log(data);

  //console.log("ID:",tweet_id);
  request.get(tweet_id)
  .end(function(err,res){
      if(err){
        console.log("Error: "+err);
      }else{
        //console.log(res.body);
        io.emit('tweet',res.body);
      }
  });
  //console.log("Tweet: ",data);
});
stream.on('error', function(err){
  console.log("Error retrieving tweets: "+err);
});
io.on('connection', function(client){
  client.on("join", function(data){
    console.log(data);
  });
  client.emit("join",{"message":"Welcome"});
});
/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port &gt;= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var serverName=cfg.server;
  var runServer=`http://localhost:8060/service/tweets/${server.address().port}`;
  if(cfg.hosted){
    runServer=`http://${serverName}/service/tweets/${server.address().port}`;
  }
  console.log(" Server on:",runServer)
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
  var expose=()=&gt;{
      request.put(blogServer,(err,res)=&gt;{
        if(err){
          console.log("Error:",err);
        }
      });
  }
  setInterval(expose,15*1000);
}
If you want to write production-ready code using node, then you should use node modules to divide your code into independent, maintainable modules. Even better, use a framework (Express). It provides "development" and "productions" options for the application and it also solves the trivial common issues for you, like setting up a router. You can also use an scaffolding tool, to generate a standard folder structure for your app.

For reading values from a config file, create a new module and store all the application wide configuration in it, instead of reading directly from a .json file.

Coming to onListening() function, it's not a good practice to hardcode url's. It's better to store them in a config module and require it when needed.