node.js上的narf模块更改默认主机名

时间:2014-07-03 21:14:29

标签: javascript node.js

我正在尝试在node.js上使用Narf模块来建立实时连接。使用以下服务器端代码设置所有内容。

    var narf = require( './narf' );

    var connectedClients = []; //keep track of which clients are connected

    var APIFunctions = { //forward facing functions

 GET : {  //headers object and parsed url are passed as a parameter for get functions

  sendToClients : function ( header, url ){

   connectedClients.forEach( function( connection ){

    connection.send( JSON.stringify( { message : url.message } ) );
   });

  }
 },

 POST : {}
};

narf.startHTTPServer( APIFunctions, function( httpServer ){


narf.narfSocketServer( httpServer, function( request ){

 var connection = request.accept( null, request.origin ); //accept the connection request

 connectedClients.push( connection );
 console.log( connectedClients.length + ' connections open' );

 connection.on( 'message', function( message ){ //the user has sent a message

  if ( message.type === 'utf8' ){

   console.log( message ); //process

   if( typeof message === 'string' ) message = JSON.parse( message );

   connection.send( JSON.stringify({ message : 'hello client' }) );
  }

 } );

 connection.on( 'close', function( ){ //The user has closed the connection

  for (var i in connectedClients){

   /* remove the client connection from the array and free some memory*/
   if( connectedClients[i] == connection ){

    connectedClients.splice(i,1);
    console.log('removing from disconnected client list');
   }
  }

 } );

} );
} );

但是,narf的默认主机名是 localhost 。我需要将主机名更改为narf的IP地址。

我无法找到更改主机名的文档。

1 个答案:

答案 0 :(得分:0)

之前我没有使用NARF,但看起来它正在运行它的机器上设置HTTP服务器。

因此,主机名不是由NARF或您的代码决定的,而是由您的计算机和网络IP和DNS配置决定的。如果您希望服务器使用特定的IP地址运行,则必须确保已为该计算机分配了该IP地址。

现在,如果你有多个IP分配给你的机器而你只想让NARF听其中一个,那就是另一回事了。我不确定NARF或Node.js是否提供了绑定多宿主计算机上特定IP的机制。