最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a way to import strings from a text file in javascript, meteor? - Stack Overflow

programmeradmin5浏览0评论

I have a programm where I need to have long multi line strings. It's a pain to store them in the .js document, because js doesn't have multi line strings and I end up having a twice as long as the screen width line looking as ugly as "This is an example.\n"

Is there a way to have a txt file, from where I can import strings with new lines (or at least just import strings)?

I have a programm where I need to have long multi line strings. It's a pain to store them in the .js document, because js doesn't have multi line strings and I end up having a twice as long as the screen width line looking as ugly as "This is an example.\n"

Is there a way to have a txt file, from where I can import strings with new lines (or at least just import strings)?

Share Improve this question asked Jul 3, 2013 at 16:53 EupheEuphe 3,7097 gold badges41 silver badges71 bronze badges 3
  • More information about how you are rendering the string would be helpful. Is it html? Is it in a dialog box? What does the program do? – Dropzilla Commented Jul 3, 2013 at 16:57
  • 1 possible duplicate of How do I load the contents of a text file into a javascript variable? – zurfyx Commented Jul 3, 2013 at 20:33
  • 2 Nope, definitely not a duplicate; this is much harder to do in Meteor and a feature that isn't implemented yet. – Andrew Mao Commented Jul 4, 2013 at 16:27
Add a ment  | 

3 Answers 3

Reset to default 7

There is a Meteor Assets object that allows you to read files in the private directory of your app, in the following way for example for text files.

Assets.getText("foo.txt", function (err, res) { ... });

See full documentation: http://docs.meteor./#assets

Previous answer works only for public files. If you want to access file data that is visible only on the server you should probably use 'fs' npm module. It's described in details here: http://www.eventedmind./posts/meteor-file-uploader-part-2-server-side-save

The meteor-yaml package makes this easy - it automatically loads any .yaml files in your project, parses them into JavaScript objects, and makes them available in YAML.data.

In my application I have some code outside of the meteor app that needs the same settings, so I prefer to have the config file outside of the meteor project directory. Then I load the file like this:

var fs = Npm.require('fs');
fs.readFile('<path to file>.yaml', 'utf8', function(err, data) {
  if(err) { 
    //Throw exception if the file is missing
    throw new Error("Missing config file")
  }
  else {
    //Read the file into a JavaScript object
    config = YAML.parse(data); 
  }
});

Unfortunately, the meteor-yaml package is a little out of date with how the meteor team wants node packages to be loaded now, so if you're using a recent version of meteor the package won't work out of the box.

I filed a bug about this, but in the meantime to get around it I installed it as a private package, as opposed to installing it from atmosphere, and fixed the bug. To do this:

  1. Clone the repo under your projects packages/ directory
  2. Comment out the Npm.require lines.
  3. Add a call to depends:

    Npm.depends({yamljs: "0.1.4"});

  4. Run meteor. Meteor will detect the meteor-yaml private package and install the dependencies.

发布评论

评论列表(0)

  1. 暂无评论