michael michael - 5 months ago 127
Node.js Question

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

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.

Answer

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