简体   繁体   中英

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.

How do I go about reading and writing files to the server?

Within the Node fs module you have a writeFile function.

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.

All this of course is server side.

you can try to use Npm.require inside the startup function. 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

an added advantage is that you can distribute everything over many nodes of a MongoDB cluster

to manipulate image files, you can use imagemagick with nodejs this should allow you to transform in any way you need.

The node fs module is a start. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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