Nodejs 有人遇到这个报错吗

Nodejs 有人遇到这个报错吗

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

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

8 回复

在文件目录上读操作?


谷歌第一条

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.

#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 服务器已启动’)
});

#2 我单独打印了 path.join(__dirname, ‘pages’, req.ur ) 发现拼接得没有问题啊

知道什么问题了。。。 是 req.url 这个 为什么 windows 打开 127.0.0.1:9999 会直接跳转到 127.0.0.1:9999/index.html mac 上如果打开就是 127.0.0.1:9999。。。

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 上有啥不同了

#6 老哥 知道什么问题了。。。 是 req.url 这个 为什么 windows 打开 127.0.0.1:9999 会直接跳转到 127.0.0.1:9999/index.html mac 上如果打开就是 127.0.0.1:9999。。。所以路径不对 直接报错了。。

针对您提到的Node.js报错问题,由于您没有提供具体的错误信息或代码片段,我将给出一些常见的Node.js错误及其可能的解决方案。如果您能提供具体的错误信息,我将能给出更精确的帮助。

常见Node.js错误及解决方案

  1. SyntaxError:通常由于代码书写错误,如拼写错误、缺少括号或引号。

    // 错误示例
    console.log('Hello World;
    // 正确示例
    console.log('Hello World');
    
  2. ReferenceError:变量未定义。

    // 错误示例
    console.log(a); // a未定义
    // 正确示例
    let a = 10;
    console.log(a);
    
  3. TypeError:操作或函数应用于错误的数据类型。

    // 错误示例
    let num = '10';
    console.log(num * 2); // TypeError: num is not a number
    // 正确示例
    let num = 10;
    console.log(num * 2);
    
  4. ModuleNotFoundError:模块未找到,通常由于路径错误或未安装模块。

    npm install express // 安装express模块
    
    // 使用模块
    const express = require('express');
    

如果上述解决方案未能解决您的问题,请提供具体的错误信息或代码片段,以便进一步分析。

回到顶部