简体   繁体   English

如何使用Meteor读取和写入服务器的文件?

[英]How do I read and write files to the server with Meteor?

I'm working on a NoDB CMS in Meteor, but I'm new to both Meteor and JavaScript frameworks. 我正在Meteor中使用NoDB CMS,但我是Meteor和JavaScript框架的新手。

How do I go about reading and writing files to the server? 如何读取和写入文件到服务器?

Within the Node fs module you have a writeFile function. 在Node fs模块中,您有一个writeFile函数。

getUser = Meteor.users.findOne({_id : Meteor.userId()});
userObject = JSON.stringify(getUser);
var path = process.env["PWD"] + "/public/";
fs.writeFile(process.env["PWD"] + "/public/"+Meteor.userId()+'.txt', userObject, 
        function (err) {
            if (err) throw err;
              console.log('Done!');
        }
    );

The above snippet would create a file with all the information of the user. 上面的代码片段将创建一个包含用户所有信息的文件。 You could access the properties of the result of your query with something like getUser._id to prepare your data parameter (String or Buffer) to print pretty. 您可以使用getUser._id类的getUser._id访问查询结果的属性,以准备您的数据参数(字符串或缓冲区)以进行漂亮打印。

All this of course is server side. 所有这些当然都是服务器端。

you can try to use Npm.require inside the startup function. 你可以尝试在启动函数中使用Npm.require。 Like so 像这样

Meteor.startup(function () {
   fs = Npm.require('fs');
}

But you should definitely have a look at collectionFS that does what you are looking for: storing files on the server and allowing you to retrieve them 但是你应该看看那些能够满足您需求的collectionFS :在服务器上存储文件并允许您检索它们

an added advantage is that you can distribute everything over many nodes of a MongoDB cluster 另一个优点是,您可以在MongoDB集群的许多节点上分发所有内容

to manipulate image files, you can use imagemagick with nodejs this should allow you to transform in any way you need. 要操作图像文件,你可以将imagemagick与nodejs一起使用这应该允许你以任何你需要的方式进行转换。

The node fs module is a start. 节点fs模块是一个开始。 http://nodejs.org/api/fs.html http://nodejs.org/api/fs.html

You might want to be a bit more specific with your question though, as it's kind of broad. 您可能希望对您的问题更具体一些,因为它有点广泛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM