nodejs服务器应用程序有类型错误,如path.join

时间:2014-04-04 19:43:28

标签: javascript node.js express webrtc

当我正在运行我的server.js时,它开始很好,主要的盯着页面被加载但我必须输入一个昵称进入任何房间。当我输入名称并单击“创建聊天室”按钮时,它会显示此类错误。由于我是构建node.js应用程序的新手,请帮我解决这个问题。服务器代​​码如下所示。

TypeError: Arguments to path.join must be strings
        at path.js:360:15
        at Array.filter (native)
        at exports.join (path.js:358:36)
        at exports.send (/home/gauz/Desktop/hackview-master/node_modules/connect/lib/middleware/static.js:129:20)
        at ServerResponse.res.sendfile (/home/gauz/Desktop/hackview-master/node_modules/express/lib/response.js:186:3)
        at app.get.req.session.nick (/home/gauz/Desktop/hackview-master/server.js:71:9)
        at callbacks (/home/gauz/Desktop/hackview-master/node_modules/express/lib/router/index.js:272:11)
        at param (/home/gauz/Desktop/hackview-master/node_modules/express/lib/router/index.js:246:11)
        at param (/home/gauz/Desktop/hackview-master/node_modules/express/lib/router/index.js:243:11)
        at pass (/home/gauz/Desktop/hackview-master/node_modules/express/lib/router/index.js:253:5)
        at Router._dispatch (/home/gauz/Desktop/hackview-master/node_modules/express/lib/router/index.js:280:5)
        at Object.Router.middleware [as handle] (/home/gauz/Desktop/hackview-master/node_modules/express/lib/router/index.js:45:10)
        at next (/home/gauz/Desktop/hackview-master/node_modules/connect/lib/http.js:204:15)
        at next (/home/gauz/Desktop/hackview-master/node_modules/connect/lib/middleware/session.js:322:9)
        at /home/gauz/Desktop/hackview-master/node_modules/connect/lib/middleware/session.js:341:9
        at /home/gauz/Desktop/hackview-master/node_modules/connect/lib/middleware/session/memory.js:52:9
        at process._tickCallback (node.js:415:13)

服务器代码:

var express=require('express'),
    app = express.createServer(),
    sharejs = require('share'),
    sharejsOptions={db:{type:'none'}};
/*
var express=require('express');
var app = express();
var sharejs = require('share');
var sharejsOptions={db:{type:'none'}};
*/
var env = process.env.NODE_ENV;

//if we were provided redis options, use them for persistence
//the if case is for developers not using redis
if(process.env.redis_port){
    sharejsOptions.db= {
        type: 'redis',
        prefix: '',
        port: process.env.redis_port,
        auth: process.env.redis_auth || null
    }
};

sharejs.server.attach(app, sharejsOptions);//attach to express

if (env !== 'production')
  app.use(express.logger('dev'));

app.use(express.static(__dirname + '/public'));
app.use(express.favicon(__dirname+"/public/favicon.ico"));
app.use(express.cookieParser());
app.use(express.session({secret:"SuperSecretSessionKey"}));

//heroku support
var port = process.env.PORT || 8000;
app.listen(port);
console.log('App running on port : '+port);

//webRTC Stuff
var webRTC = require('webrtc.io').listen(app);
require('./rtc.js')(webRTC);

app.get('/', function(req, res) {
  res.sendfile(__dirname + '/public/room.html');
});

/** Create a new random room */
app.get('/join',function(req,res){
  var roomName=req.query.nickname.split('@')[1];
  if(!roomName)
    roomName=getRandomRoom();
  var nickName = req.query.nickname.split('@')[0];
  req.session.nick = nickName;
  res.redirect('/room/'+roomName);
});

var getRandomRoom = function(){
  var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  var string_length = 8;
  var randomstring = '';
  for (var i=0; i<string_length; i++) {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
  }
  return randomstring;
}

app.get('/room/:roomName',function(req,res){
  if(req.session.nick || req.query.asknick){
    //if a person has his nickname set, let him reach that
    res.sendfile(__dirname+'/public/room.html');
  }
  else{
    //make sure he/she is asked a username
    res.redirect('/room/'+req.params.roomName+'?asknick=yes');
  }
});

app.get('/setnick',function(req,res){
  res.cookie('nick',req.query.nick);
  req.session.nick = req.query.nick;
  res.send('');
});

app.get('/debug',function(req,res){
  res.json(req.session);
});

1 个答案:

答案 0 :(得分:1)

根据this GitHub issue,您需要将res.sendfile的来电替换为:

res.sendfile(__dirname+'/public/room.html');

到此:

res.sendfile('/public/room.html', { root: __dirname });