CNode

小错误:TypeError: Arguments to path.join must be strings, 求真相

Lljgstudy发布于12 年前最后回复12 年前11 回复7039 浏览0 收藏

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

主要的原因是下面两段代码: 第一中写法:

第二种写法:

用第一段代码会出错,改成第二种写法就没问题了。 这是怎么个情况呢,求真相。。。

查看回复

回复 (11)

Y
youxiachai#1·12 年前

嗯, 看 send 模块 (sendfile 的依赖包)

https://github.com/visionmedia/send

在win 下 你传

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

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

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

L
ljgstudy#2·12 年前
A
alsotang#5·12 年前
引用 youxiachai嗯, 看 send 模块 (sendfile 的依赖包) https://github.com/visionmedia/send 在win 下 你传 dirname + '/index.htm...

__dirname + '/index.html' 这句改成 path.join(__dirname, '/index.html') 应该就可以了吧?

M
mondwan#7·12 年前
引用 youxiachai嗯, 看 send 模块 (sendfile 的依赖包) https://github.com/visionmedia/send 在win 下 你传 dirname + '/index.htm...

@ljgstudy

//path.join(__dirname, '/index.html')

path.join(__dirname, 'index.html')

試試不用那個'/'

L
ljgstudy#8·12 年前
M
mondwan#9·12 年前
引用 youxiachai嗯, 看 send 模块 (sendfile 的依赖包) https://github.com/visionmedia/send 在win 下 你传 dirname + '/index.htm...

@ljgstudy 嗯... 我發現你應該在用2.0 的express connect 用的應該是1.x 根源 https://github.com/senchalabs/connect/blob/1.x/lib/middleware/static.js#L104

root = options.root ? normalize(options.root) : null

https://github.com/senchalabs/connect/blob/1.x/lib/middleware/static.js#L129

path = normalize(join(root, path));

不設定root 就是null, join(null, path) => errors

L
ljgstudy#10·12 年前
引用 youxiachai嗯, 看 send 模块 (sendfile 的依赖包) https://github.com/visionmedia/send 在win 下 你传 dirname + '/index.htm...

@mondwan 说的对。我刚才验证了一把,结果如下: root = null; path = E:\nodejs\demo\sending-nickname\index.html 然后执行 path = normalize(join(root, path)); 出错,与windows路径分隔符无关! 谢谢,长知识了。。。

A
alsotang#11·12 年前
引用 youxiachai嗯, 看 send 模块 (sendfile 的依赖包) https://github.com/visionmedia/send 在win 下 你传 dirname + '/index.htm...

@ljgstudy 额。。。楼上说了那么多,你就始终不能自己打印 path.join(__dirname, '/index.html') 出来看看吗。。

参与回复
登录后即可参与回复。登录