nodejs 有人遇到这个报错吗

2019-08-11 11:59:39 +08:00
 wumao

{ [Error: EISDIR: illegal operation on a directory, read] errno: -21, code: 'EISDIR', syscall: 'read' }

在 windows 上运行没问题 mac 就报这个错 百度谷歌都翻遍了。。node 初学者

8878 次点击
所在节点    Node.js
7 条回复
William911
2019-08-11 12:01:36 +08:00
在文件目录上读操作?
df4VW
2019-08-11 12:19:37 +08:00
谷歌第一条

EISDIR means that the target of the operation is a directory in reality but that the expected filetype of the target is something other than a directory.
wumao
2019-08-11 12:46:21 +08:00
@William911 #1 是的 代码如下 然后目录文件什么的都是存在的 在 windows 上一点问题都没

const http = require('http');
const fs = require('fs');
const path = require('path');

const server = http.createServer();

server.on('request', (req, res) => {
console.log(req.url);
fs.readFile(path.join(__dirname, 'pages', req.url ), (err, data) => {
if (err) {
return console.log(err);
}
res.end(data);
})
});

server.listen(9999, () => {
console.log('http://localhost:9999/index.html 服务器已启动')
});
wumao
2019-08-11 12:48:02 +08:00
@df4VW #2 我单独打印了 path.join(__dirname, 'pages', req.ur ) 发现拼接得没有问题啊
wumao
2019-08-11 12:57:05 +08:00
知道什么问题了。。。 是 req.url 这个 为什么 windows 打开 127.0.0.1:9999 会直接跳转到 127.0.0.1:9999/index.html mac 上如果打开就是 127.0.0.1:9999。。。
df4VW
2019-08-11 12:58:31 +08:00
When the path is a directory, the behavior of fs.readFile() and fs.readFileSync() is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned.

mac 上报错没啥问题,因为你试着读一个 directory,理论上你 windows 也应该报错,看你 windows 上有啥不同了
wumao
2019-08-11 12:59:50 +08:00
@df4VW #6 老哥 知道什么问题了。。。 是 req.url 这个 为什么 windows 打开 127.0.0.1:9999 会直接跳转到 127.0.0.1:9999/index.html mac 上如果打开就是 127.0.0.1:9999。。。所以路径不对 直接报错了。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/590902

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX