使用 Meteor 生成和提供静态文件 [英] generating and serving static files with Meteor
问题描述
我希望根据提供的对象的内容创建静态文本文件,然后用户可以下载这些文件.这是我打算做的:
I'm looking to create static text files based upon the content of a supplied object, which can then be downloaded by the user. Here's what I was planning on doing:
当用户点击导出"时,应用程序调用
Meteor.method()
,然后使用典型的 Node 方法解析文件并将其写入公共目录.
When the user hits 'export' the application calls a
Meteor.method()
which, in turn, parses and writes the file to the public directory using typical Node methods.
创建文件后,在来自 Meteor.method()
的回调中,我提供了一个指向生成文件的链接.例如,'public/userId/file.txt'.然后,用户可以选择从该链接下载文件.
Once the file is created, in the callback from Meteor.method()
I provide a link to the generated file. For example, 'public/userId/file.txt'. The user can then choose to download the file at that link.
然后我使用 Meteor 的 Connect modele
(它在内部使用)将任何对上述 URL 的请求路由到文件本身.我可以根据 userId 和用户的登录状态进行一些权限检查.
I then use Meteor's Connect modele
(which it uses internally) to route any requests to the above URL to the file itself. I could do some permissions checking based on the userId and the logged in state of the user.
问题:公开生成静态文件时,网页每次都会自动重新加载.我认为使用 Express 之类的东西来生成 REST 端点可能更有意义,它可以处理创建文件.但是如果我无权访问 Meteor 会话数据,我不确定如何处理权限.
The problem: When static files are generated in public, the web page automatically reloads each time. I thought that it might make more sense to use something like Express to generate a REST endpoint, which could deal with creating the files. But then I'm not sure how to deal with permissions if I don't have access to the Meteor session data.
对这里的最佳策略有什么想法吗?
Any ideas on the best strategy here?
推荐答案
Symlink hack 将不再适用于 Meteor(从 0.6.5 开始).相反,我建议使用与以下类似的代码创建一个包:
The symlink hack will no longer work in Meteor (from 0.6.5). Instead I suggest creating a package with similar code to the following:
packge.js
Package.describe({
summary: "Application file server."
});
Npm.depends({
connect: "2.7.10"
});
Package.on_use(function(api) {
api.use(['webapp', 'routepolicy'], 'server');
api.add_files([
'app-file-server.js',
], 'server');
});
app-file-server.js
var connect = Npm.require('connect');
RoutePolicy.declare('/my-uploaded-content', 'network');
// Listen to incoming http requests
WebApp.connectHandlers
.use('/my-uploaded-content', connect.static(process.env['APP_DYN_CONTENT_DIR']));
这篇关于使用 Meteor 生成和提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!