Meteor:尝试在/public 之外提供静态文件 [英] Meteor: Trying to serve a static file outside of /public
问题描述
TLDR;如何通过铁路由器提供静态文件?如果我将文件放在/public 中,它会绕过我可以在铁路由器中使用的所有钩子.
TLDR; How can one serve a static file through iron router? If I place a file in /public it bypasses all the hooks I could use in iron router.
长版:我需要将所有请求记录到图像文件中.具体来说,我想在每个请求中坚持到 Mongodb,例如来自 http://domain.com/cat.png?a=1&b=2.
Long version: I need to log all requests to an image file. Specifically I'd like to persist to Mongodb the querystring in every request such as 'a=1&b=2' from http://domain.com/cat.png?a=1&b=2.
我在 Meteor 中没有看到这样做的方法.但是我可以使用铁路由器挂钩,例如 params.query .除了/public 中的任何静态文件都由 Meteor 的处理程序提供服务,并绕过铁路由器.
I don't see a way of doing this in Meteor. But I could use the iron router hooks such as params.query . Except that any static files in /public get served by Meteor's handler, and bypasses iron router.
(我希望使用最新的 api.这里有很多关于流星 1.0 和 Ironrouter 前的帖子)
(I'm looking to use up to date apis. There are lots of posts here pre meteor 1.0 and pre ironrouter)
推荐答案
您可以使用 fs
来服务任何文件:
You can use fs
to server any file:
Router.route('/static/:filename', function (){
var fs = Npm.require('fs'),
path = '/tmp/' + this.params.filename; // make sure to validate input here
// read the file
var chunk = fs.createReadStream(path);
// prepare HTTP headers
var headers = {}, // add Content-type, Content-Lenght etc. if you need
statusCode = 200; // everything is OK, also could be 404, 500 etc.
// out content of the file to the output stream
this.response.writeHead(statusCode, headers);
chunk.pipe(this.response);
});
这篇关于Meteor:尝试在/public 之外提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!