首页 > 解决方案 > 使用 Node js 读取文件时出错

问题描述

我无法使用我在互联网上找到的此代码读取文件...

var express = require('express');
var app = express();
var fs = require('fs');
var port = 8080;

app.get('/', function(req, res) {
  fs.readFile('./queries/anagconti_clienti_giorni.txt', 'utf8', function(err, data) {
    if (err) {
      res.send(err);
    } else {
      res.send(data);
    }
  });
});

var server = app.listen(port, function() {
  console.log("Started on http://localhost:" + port);
});

它给我的错误是:

{
“errno”:-4058,
“代码”:“ENOENT”,
“系统调用”:“打开”,
“路径”:“C:\Users\AlessandroGolin\Desktop\Mia\Visual_Studio\server_link\queries\anagconti_clienti_giorni.txt
” }

什么可能导致此错误?

标签: node.jsfilecompiler-errorsreadfile

解决方案


请交叉检查“./queries/anagconti_clienti_giorni.txt”文件的路径或权限。如果文件不存在,则在上述代码文件所在的同一级别创建“查询”文件夹。


推荐阅读