V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wumao
V2EX  ›  Node.js

nodejs 有人遇到这个报错吗

  •  1
     
  •   wumao · 2019-08-11 11:59:39 +08:00 · 8868 次点击
    这是一个创建于 1712 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    William911
        1
    William911  
       2019-08-11 12:01:36 +08:00 via iPhone
    在文件目录上读操作?
    df4VW
        2
    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
        3
    wumao  
    OP
       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
        4
    wumao  
    OP
       2019-08-11 12:48:02 +08:00
    @df4VW #2 我单独打印了 path.join(__dirname, 'pages', req.ur ) 发现拼接得没有问题啊
    wumao
        5
    wumao  
    OP
       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
        6
    df4VW  
       2019-08-11 12:58:31 +08:00   ❤️ 1
    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
        7
    wumao  
    OP
       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。。。所以路径不对 直接报错了。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1108 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:44 · PVG 02:44 · LAX 11:44 · JFK 14:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.