无法在Ubuntu服务器上运行节点服务器文件

时间:2015-08-04 10:33:10

标签: node.js ubuntu

我无法在Ubuntu服务器上启动我的server.js文件。我成功安装了所有npm软件包。但是当我运行命令node server.js时脚本根本没有执行,也没有错误。点击后输入它再次移回putty ssh中的文件夹路径。我正在解释下面的代码。

  

server.js:

var port=8888;
var express=require('express');
var morgan = require('morgan');
var http=require('http');
var bodyParser= require('body-parser');
var methodOverride = require('method-override');
var mongo = require('mongojs');
var database='Oditek';
var collections=['video'];
var app= express();
var server=http.Server(app);
var io=require('socket.io')(server);
var db = mongo.connect("127.0.0.1:27017/"+database, collections);
app.use(express.static(__dirname + '/public'));     // set the static files location /public/img will be /img for users
app.use(morgan('dev'));                     // log every request to the console
app.use(bodyParser.urlencoded({ extended: false }))    // parse application/x-www-form-urlencoded
app.use(bodyParser.json())    // parse application/json
app.use(methodOverride());                  // simulate DELETE and PUT
db.on('ready', function () {
    console.log('database connected')
});
app.get('/',function(req,res){
    res.sendfile('view/login.html');
});
app.post('/login',function(req,res){
    var username=req.body.username;
    var password=req.body.userpassword;
    if(username && password){
        db.video.findOne({
            username:username,
            password:password
        },function(err,doc){
            if(doc){
                console.log('login',doc);
                res.send(doc);
            }
            if(err){
                console.log('login12',err);
                res.send("could not login");
            }
        });
    }
});
app.get('/index',function(req,res){
    res.sendfile('view/index.html');
});
app.get('/video',function(req,res){
    res.sendfile('view/video.html');
});
app.get('/whiteboard',function(req,res){
    res.sendfile('view/whiteboard.html');
});
//socket----programming//
var roomid;
var clients={};
io.on('connection',function(socket){
    //console.log('socket id',socket);
    if(socket.handshake.query.roomid){
      roomid=socket.handshake.query.roomid;
    }
    var usertype=socket.handshake.query.usertype;
    //var url=socket.handshake.headers.referer;
    //var myString = url.substr(url.indexOf("?") + 1);
    //var usertype=myString.substr(myString.indexOf("=")+1);
    //console.log('usertype',usertype);
    clients[usertype]={
        "socket":socket.id
    }
    console.log('clients',clients['admin'].socket);
    socket.on('admin-join',function(data){
        if(data.IsJoinAdmin){
            socket.join(roomid);
        }
    });
    socket.on('user-join',function(data){
        console.log('user wants to join',data);
        //console.log('user type',clients);
        if(data.isUserJoin){
            io.sockets.connected[clients[data.usertype].socket].emit('user-already-joined',data);
            socket.join(roomid);
        }
    });
    socket.on('send-playing-video-request-to-users',function(data){
        io.to(roomid).emit('sending-broadcasting',data);
    });
    socket.on('send-broadcasting-message',function(data){
        io.to(roomid).emit('sending-broadcasting',data);
    });
    socket.on('share-white-board',function(msg){
        io.to(roomid).emit('sharing-white-board',msg);
    });
    socket.on('disconnect', function() {
    for(var user in clients) {
        if(clients[user].socket === socket.id) {
            delete clients[user];
            io.to(roomid).emit('user-has-left',{userleft:true});
            break;
        }
    }   
  })

});
server.listen(port);
console.log('server is listening on the port'+port);

如果此文件正在运行,至少应该执行控制台消息。但我不是那样的。请帮我解决此错误。

1 个答案:

答案 0 :(得分:0)

还有另一个名为node的程序,当调用名为"业余数据包比率节点程序"时,它将不显示输出。通过与您聊天,我确定这是您系统上的内容。有两种基本解决方案。

  1. 删除" Armature Packet Ratio节点",然后重新安装node.js
  2. sudo apt-get purge node nodejs  # Uninstall both
    sudo apt-get install nodejs  # Reinstall nodejs
    
    1. 使用这两个程序并通过node.js访问node.js
    2. nodejs server.js
      forever -c 'nodejs server.js' start