[Node.js]在windows下不得不防的小错误

2020-11-24 23:09

阅读:557

标签:node.js   socket.io   typeerror   

TypeError: Arguments to path.join must be strings
    at f (path.js:204:15)
    at Object.filter (native)
    at exports.join (path.js:209:40)
    at exports.send (E:\nodejs\demo\socket.io-express\node_modules\express\node_modules\connect\lib\middleware\static.js:129:20)
    at ServerResponse.res.sendfile (E:\nodejs\demo\socket.io-express\node_modules\express\lib\response.js:186:3)
    at io.sockets.on.socket.emit.text (E:\nodejs\demo\socket.io-express\app.js:8:6)
    at callbacks (E:\nodejs\demo\socket.io-express\node_modules\express\lib\router\index.js:272:11)
    at param (E:\nodejs\demo\socket.io-express\node_modules\express\lib\router\index.js:246:11)
    at pass (E:\nodejs\demo\socket.io-express\node_modules\express\lib\router\index.js:253:5)
    at Router._dispatch (E:\nodejs\demo\socket.io- express\node_modules\express\lib\router\index.js:280:5)

Node.js中的两个模块Socket.io 和Express做整合的时候出现了以下的错误:


var express = require(‘express‘);
var app = module.exports = express.createServer(),
    io = require(‘socket.io‘).listen(app);
 
app.listen(3000);
 
app.get(‘/‘, function(req, res){
    res.sendfile(__dirname + ‘/index.html‘);
});
 
io.sockets.on(‘connection‘, function(socket){
    socket.emit(‘welcome‘, {text: ‘OH HAI! U R CONNECTED!‘});
});

错误原因:

主要的错误原因是上面代码第8行,将代码改为以下形式即可工作。

var express = require(‘express‘);
var app = module.exports = express.createServer(),
    io = require(‘socket.io‘).listen(app);
app.listen(3000);

app.get(‘/‘, function(req, res){
	res.sendfile(‘/index.html‘, {root: __dirname});
});

io.sockets.on(‘connection‘, function(socket){
	socket.emit(‘welcome‘, {text: ‘OH HAI! U R CONNECTED‘});
});


在win 下 传

__dirname + ‘/index.html‘ -> E:\nodejs\demo/index.html

貌似,那里用 url 解析模块.最后解析的结果不是windows 那种路径样子..

然后到创建流的时候路径就不对了..

参考资料:https://github.com/visionmedia/send


[Node.js]在windows下不得不防的小错误,搜素材,soscw.com

[Node.js]在windows下不得不防的小错误

标签:node.js   socket.io   typeerror   

原文地址:http://blog.csdn.net/ljgstudy/article/details/25479557


评论


亲,登录后才可以留言!