How can I fs.readFile() a file from the /public folder of a meteor-up deployment?

时间:2016-06-18 20:10:19

标签: node.js meteor fs meteor-up

I have a meteor-up/mupx deployment with a page in the Meteor /public folder, ./public/index.html

I can load the page successfully from a url that is something like this:

http://my-app.com:3333/index.html

But I would like to make this page the default page for the domain, so I can access it like this:

http://my-app.com:3333/

I added the following in my Meteor bootstrap.js file

WebApp.connectHandlers.use("/", function(req, res, next) {
  var err, error, error1, filepath0, filepath1;
  if (req.originalUrl !== '/' || req.method !== 'GET') {
    return next();
  }
  try {
    filepath0 = process.env.PWD + '/public/index.html';
    // filepath1 = '/opt/my-app/current/bundle/programs/web.browser/app/index.html';
    fs.statSync(filepath0).isFile();
  } catch (error) {
      err = error1;
      console.log("file not available, path=" + filepath);
      return next();
  }
  fs.readFile(filepath0, function(err, buf) {
    var eTag, err, headers;
    try {
      eTag = crypto.createHash('md5').update(buf).digest('hex');
      if (req.headers['if-none-match'] === eTag) {
        res.writeHead(304, 'Not Modified');
        return res.end();
      }
    } catch (err) {
      eTag = null;
    }
    headers = {
      'Content-Type': 'text/html',
      'ETag': etag
    };
    res.writeHead(200, headers);
    return res.end(buf);
  });
});

This works fine from my standard Meteor project. But when I deploy via mupx I get an exception because the index.html file is not in the same location.

How can I fs.readFile() the file which was located in /path/to/Meteor/public after it has been deployed via mupx

from Meteor, I could use `filepath0 = process.env.PWD + '/public/index.html'

But the same code from mupx gives filepath0 = /bundle/bundle/public/index.html, and the file is not there.

from the mupx server, I can actually ls the file at filepath1, but even using that value does not work.

3 个答案:

答案 0 :(得分:1)

我通过一些反复试验弄明白了。感谢@quirk指出我正确的方向。还有,console.log(fs.readdirSync('.'))

" / public"的路径meteor-up部署中的文件夹是:

// console.log(fs.readdirSync('.')) => '/opt/my-app/current/bundle/programs/server' public_folder = '../../programs/web.browser/app' filepath = '../../programs/web.browser/app/index.html'

答案 1 :(得分:0)

你在问题​​中自己给出了答案。您只需拥有正确的URL,具体取决于您的部署状态(开发或生产)。只需在mupx配置文件中设置一个新的环境变量,然后在生成process.env的网址之前在index.html中查找该环境变量。

除了以上是解决问题的快速而肮脏的黑客之外,我觉得你正以完全错误的方式接近这个。如果您访问网站时index.html需要成为您的默认页面(是的,这就是路线' /'等同于什么),为什么不是您的client/main.html

如果由于某些原因我们不知道,您无法使用客户端文件夹,只需在项目根目录中保留main.html并将其设置为使用简单的HTML重定向重定向到/index.html。什么是冷生成你自己的页面响应?!

答案 2 :(得分:0)

IronRouter具有服务器端路由。见h / t Serving an "index.html" file in public/ when using MeteorJS and Iron Router?

这就是我做的工作:

设置文件:

# copy /path/to/ionic/www to /path/to/meteor/public
# put ./public/index.html into ./private
cd /path/to/meteor/private; ln -s ../public/index.html .;

设置路由:

# /path/to/meteor/server/bootstrap.js
Router.route('/', {
  where: 'server'
}).get(function() {
  var contents;
  contents = Assets.getText('index.html');
  return this.response.end(contents);
});
相关问题