node.js Meteor 文件系统的问题

node.js issues with Meteor's file system

我试图找出我在这个谜题中遗漏了什么。 Node.js 和 Meteor.js。 Meteor 建立在 Node.js 之上,我知道这一点。但是 Meteor 不能与 Node.js 一起正常工作。要么我需要再执行 20 个步骤才能获得相同的结果,但我不知道它们是什么。或者两者之间存在严重的错误。独立 Node.js 运行下面的命令就好了。 运行 Meteor 上的相同命令会导致错误或未定义的结果。希望我知道为什么要解决这个问题,或者他们需要修补这个问题,这样它才能按应有的方式工作。

examples #1 

var fs = require('fs');
fs.readFile('file.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});

example #2

var jetpack = require('fs-jetpack');

var data = jetpack.read('file.txt');
console.log(data);

example #3

 var fs = require ('fs');

 var readMe = fs.readFileSync('file.txt', 'utf8');
 console.log(readMe);

您不应该尝试像这样加载文件,因为您不知道文件夹结构是什么样的。 Meteor 在开发和生产模式下从您的项目目录创建构建。这意味着即使您的项目文件夹中有一个 file.txt,它也不会最终出现在构建中的同一个位置(或者它甚至根本不包含在构建中)。

例如,您的代码尝试从开发构建文件夹 .meteor/local/build/programs/server 中读取文件。但是,此文件夹不包含 file.txt.

解决方案:file.txt 存储在项目的 private 文件夹中并使用 Assets.getText to read it. If you still want to use the functions from fs to load the file, you can retrieve the absolute path with Assets.absoluteFilePath.