0

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 Answers 3

1

I figured it out through some trial and error. Thanks to @quirk for pointing me in the right direction. also, console.log(fs.readdirSync('.'))

The path to the "/public" folder in a meteor-up deployment is:

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

Sign up to request clarification or add additional context in comments.

Comments

0

You gave the answer yourself in the question. You just need to have the correct URL depending on your deployment status (development or production). Just set a new environment variable in your mupx config file and look for it in process.env before your generate your URL for index.html.


Aside from the fact that the above is a quick and dirty hack to resolve your problem, I feel that you are approaching this in entirely the wrong way. If index.html needs to be your default page when you visit the site (yes that is what the route '/' is equivalent to), why is it not your client/main.html.

If for some reason unbeknownst to us, you cannot use the client folder, just keep a main.html in your project root and set it up to redirect to /index.html using a simple HTML redirect. What is the need to stone cold generate your own page response?!

2 Comments

You are right, this is hack-ish. But I'm trying to deploy and 'ionic CLI' webapp on the same domain as the Meteor backend. If there is a better way, then please share. see: stackoverflow.com/questions/37881148/…. But I'm not clear about how to use the environment variable to get the filepath of the '/public' folder.
You just look for a specific environment variable (say PROD). You define it in your mupx config file so that is shows up in your production server's environment variables. In your code you check for the variable. If it exists, then you can use a different hard-coded URL than the one you use for your development server. Your custom environment variable doesn't need to have any useful value whatsoever. It just needs to be declared so that you can find it later. It just serves similar to a boolean flag.
0

IronRouter has server side routes. see h/t Serving an "index.html" file in public/ when using MeteorJS and Iron Router?

This is what I did to make it work:

setup files:

# 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 .;

setup routing:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.