Javascript Node.JS readFileSync() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25883775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:00:22  来源:igfitidea点击:

Node.JS readFileSync() function

javascriptnode.js

提问by Xylon

index.js

索引.js

var server = require("./server");
var router = require("./router");

server.start(router.route);

server.js

服务器.js

//Script to start a server

var http = require("http");
var url = require("url");
var fs = require("fs");

function start(route) {
    function onRequest(request, response) {

        var pathname = url.parse(request.url).pathname;

        route(pathname, response, fs);

    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}

exports.start = start;

router.js

路由器.js

function route(pathname, response, fs) {

    var regex = new RegExp('^/view/?$');

    var directpath = "D:/nodejs/file_upload" + pathname;

    var voo = fs.readFileSync(directpath);

    if(regex.test(pathname)){

        response.writeHead(200, {"Content-Type": "text/html"});
        console.log("About to route a request for " + pathname);
        response.end(voo);

    } else{

        response.writeHead(404);
        response.end("<br/>404, file not found");

    }
}

exports.route = route;

index.html

索引.html

<!DOCTYPE html>
<html>
    <body>
        <p>Hello My friend </p>
    </body>
</html>

I'm trying to store the file path in a variable and then feed it ti readFileSync() function, but this gives me fllowing error in the console.

我试图将文件路径存储在一个变量中,然后将它提供给 readFileSync() 函数,但这给我带来了控制台中的错误。

Error: EISDIR, illegal operation on a directory
    at Object.fs.readSync (fs.js:487:19)
    at Object.fs.readFileSync (fs.js:326:28)
    at route (D:\nodejs\file_upload\router.js:7:15)
    at Server.onRequest (D:\nodejs\file_upload\server.js:15:6)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23
)
    at Socket.socket.ondata (http.js:1966:22)
    at TCP.onread (net.js:527:27)

but if I enter the path "D:/nodejs/file_upload/view/index.html" in the function directly then it shows me the page in the browser.

但是如果我直接在函数中输入路径“D:/nodejs/file_upload/view/index.html”,那么它会在浏览器中显示页面。

I stored the index.html file in the view folder

我将 index.html 文件存储在视图文件夹中

回答by Tom Grant

EISDIRerror occurs when you try to open a file, but the path given is a directory. See related question and answer: Using Node.js I get, "Error: EISDIR, read".

EISDIR当您尝试打开文件时发生错误,但给定的路径是一个目录。请参阅相关问答:使用 Node.js 我得到, "Error: EISDIR, read"

To debug this I would log to console the variable directpathand I would assume that it is pointing to a directory, not a file. Correctly setting this variable to the intended path should solve your problem.

为了调试这个,我会登录到控制台变量directpath,我会假设它指向一个目录,而不是一个文件。将此变量正确设置为预期路径应该可以解决您的问题。