动态添加文件到公共文件夹并在meteor中获取刷新的结果 [英] dynamically add files to the public folder and getting the refreshed results in meteor

查看:30
本文介绍了动态添加文件到公共文件夹并在meteor中获取刷新的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用 Meteor 作为后端来创建静态 html 生成器(带有管理 UI)的想法.

I'm playing around with the idea of using Meteor as a backend for creating a static html generator (with an admin UI).

我希望,当触发时,将在公共文件夹中创建一个新文件,并立即由作者在嵌入 html 的 iframe 上进行审查.

I want that, when triggered, a new file will be created on the public folder and instantly reviewed by the author on an iframe embed in the html.

文件已创建,但发生了两个副作用:

The file gets created but two side effects happen:

  1. 服务器已重新启动.
  2. 文件已缓存 - 因此用户无法看到更改发生.

有什么想法吗?

if (Meteor.is_client) {

  Template.hello.events = {
    'click input' : function () {
      Meteor.call('makeFile', 'filename.html', function(error, result){
        alert(result);
      });
     //window.location = '/filename.txt';

      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
    }
  };
}

if (Meteor.is_server) {
   var fs = __meteor_bootstrap__.require('fs');
  Meteor.startup(function () {
    // code to run on server at startup

  });
  Meteor.methods({
    'makeFile': function(fileName) {
      /*
      fs.unlink("public/"+fileName, function (err) {
        if (err) throw err;
        console.log('successfully deleted ');
      });
  */
      fs.writeFile("public/"+fileName, "<html><body><h1>test</h1></body></html>", function(err) {
        if(err) {
          console.log(err);
        } else {
          console.log("The file was saved! "+ fileName);


        }
      });
      return fileName;
    }
  });
}

推荐答案

我认为每当您更改meteor 子目录中的任何文件时,meteor 服务器都会重新启动.因此,为了防止这种情况,请不要将您的文件写入到meteor 应用程序目录中,请创建一个 created_files 目录或上一级.无论如何,这是一个好主意,将生成的文件与生成它们的代码分开.

I think the meteor server restarts whenever you change any file in a meteor subdirectory. So to prevent this don't write your files within the meteor app directory, make a created_files directory one level up or something. This is a good idea anyway, to separate the generated files from the code that's generating them.

在管理界面中显示内容的最简单方法可能是将其写入数据库(而不是尝试观察目录更改).重新发明轮子没有意义,因为 Meteor 旨在监视数据库更新.

Easiest way to show the content in the admin interface is probably to write it to the database (as opposed to trying to watch for directory changes). No sense re-inventing the wheel, since Meteor is designed to watch for db updates.

创建一个集合(在顶部,所以它在客户端和服务器上):

Create a collection (at the top, so it's on client & server):

GeneratedFiles = new Meteor.Collection("generated_files");

然后在写入文件之前将文件内容分配给一个变量,并在您的 fs.writeFile 回调中:

Then assign the file contents to a variable before writing them and in your fs.writeFile callback:

if(err) {
  console.log(err);
} else {
  GeneratedFiles.insert({name: fileName, contents: fileContents});
  console.log("The file was saved! "+ fileName);
}

然后将文件内容自动发布到客户端代码中的模板(不知道为什么要使用 iframe,似乎 div 可以正常工作.但无论哪种方式都可能有效).

And then auto-publish the file contents to template in the client code (not sure why you want to use an iframe, seems like a div would work fine. But either way would probably work).

希望有所帮助.祝你好运,我认为meteor 是这种事情的一个很好的框架.我使用的大多数管理界面仍然是完全同步的,只是使用起来很烦人.

Hope that helps. And good luck, I think meteor is a great framework for this kind of thing. Most of the admin interfaces I use are still entirely synchronous and just annoying to work with.

这篇关于动态添加文件到公共文件夹并在meteor中获取刷新的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆