[Node.js]在windows下不得不防的小錯誤

來源:互聯網
上載者:User

標籤: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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.