如何在vps中运行node.js应用程序

时间:2015-02-13 07:30:34

标签: node.js express socket.io chat

我使用express和socket.io http://socket.io/get-started/chat/

创建了一个node.js聊天应用

我在本地服务器上成功运行聊天应用程序。我必须在我的VPS中部署它。有什么步骤? 我在我的VPS&中安装了node express和socket.io。将我的所有内容上传到服务器中。

我在puTTY中运行命令$node index.js。它说听3000端口。

但是当我去那个网址时它会收到错误http://******.in/socket.io/socket.io.js 404(Not Found)

但它在我的localhost中运行得非常好。

index.js

    var app = require('express')();
    var url = require('url');
    var server = require('http').Server(app);
    var io = require('socket.io')(server);
    var numUsers=0;
    var users={};
    app.get('/', function(req, res){
      res.sendFile(__dirname + '/index.html');
    });

    server.listen(3000, function(){
      console.log('listening on *:3000');
    });

    io.sockets.on('connection',function(socket){
        console.log('connected');

        socket.on('send message',function(data,callback){
            var msg=data.trim();
            if(msg.substr(0,1)=='p'){
                msg=msg.substr(1);
                var ind=msg.indexOf(' ');
                if(ind!=-1){
                    var name=msg.substr(0,ind);
                    var msg=msg.substr(ind+1);
                    if(name in users){
                        users[name].emit=('private',{msg:msg,nick:socket.nickname});
                        console.log('private message');
                    }else{
                        callback('Enter your name');
                    }
                }else{
                    callback('Enter private message');
                }
            }else{
            console.log('message'+data);
            io.sockets.emit('new message',{msg:msg,nick:socket.nickname}); 
            }
        });

        function updateNickNames(){
            io.sockets.emit('usernames',Object.keys(users));        
        }
        function countUsers(){
            io.sockets.emit('users count',{userscount:numUsers});
        }
        socket.on('new user',function(data,callback){

               if(data in users){
                  callback(false);
               }else{
                    ++numUsers;
                    callback(true); 
                    socket.nickname=data;
                    users[socket.nickname]=socket;
                    updateNickNames();
                    countUsers();
               }
        });
        socket.on('disconnect',function(data){
            if(!socket.nickname) return;
            delete users[socket.nickname];
            --numUsers;
            updateNickNames();
            countUsers();
            io.sockets.emit('remove user',{username:socket.nickname});
        });
    });

的index.html

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
    #chat{

      height: 500px;
    }
    #content{
      display: none;
    }
    #chatwrap{
      float: left;
      border: 1px #000 solid;
    }
    .error{
      color: red;
    }
    .private{
      color:red;
      font-style:italic;
    }
    </style>
  </head>
  <body>
    <div id="nickwrap">
      <p>Username</p>
      <p id="error"></p>
       <form id="setnick">
          <input type="text" id="nickname">
          <input type="submit" value="submit"/>
        </form>
    </div> 
    <div id="content"> 
      <p id="list"></p>
      <div id="chatwrap">
        <div id="chat"></div>
        <div id="users"></div>
          <form id="send-message">
            <input type="text" id="message">
            <input type="submit" value="submit"/>
          </form>     
      </div>
    </div>
       <script src="jquery.min.js"></script>
       <script src="/socket.io/socket.io.js"></script>
      <script>
         jQuery(function($){


           var socket=io.connect();
           var $nickForm=$('#setnick');
           var $nickerror=$('#error');
           var $nickname=$('#nickname');
           var $messageForm=$('#send-message');
           var $message=$('#message');
           var $chat=$('#chat');
           var $users=$('#users');
           var $list=$('#list');
           /*--client--*/
           $nickForm.submit(function(e){
              e.preventDefault();
              socket.emit('new user',$nickname.val(),function(data){
                    if(data){
                        $('#nickwrap').hide();
                        $('#content').show();
                    }else{
                       $nickerror.html('username exists');
                    }
              });
              $nickname.val('');

           });
           $messageForm.submit(function(e){

             e.preventDefault();
             socket.emit('send message',$message.val(),function(data){
              $chat.append('<span class="error">'+data+'</span><br\>');    
             });
             $message.val('');
           });
           /*--end client--*/
           /*---server--*/
           socket.on('users count',function(data){
              var count='';
             count+='There are<b>'+data.userscount+'</b>take part in chat';
             $list.html(count);
           });
           socket.on('remove user',function(data){
              $users.append(data.username+'leftchat<br>');
           });
           socket.on('new message',function(data){
             $chat.append("<b>"+data.nick+":</b>"+data.msg+"<br\>");
             console.log('no'+data.userscount);

           });
           socket.on('usernames',function(data){
            var html='';
            for(i=0;i<data.length;i++){
              html+=data[i]+"Joined</br>";
            }
            $users.html(html);
           });
           socket.on('private',function(data){
              console.log("private");
              $chat.append('<span class="private">hello private'+data.nick+':'+data.msg+'</span><br\>');
           });
           /*--end server --*/
         });
      </script>
  </body>
</html>

0 个答案:

没有答案