Question

I am currently making a Meteor app and am having trouble reading files from the private subdirectory. I have been following a couple different tutorials and managed to get it to work flawlessly when I run the meteor app locally. This question (Find absolute base path of the project directory) helped me come up with using process.env.PWD to access the root directory, and from there I use .join() to access the private folder and the pertinent file inside. However, when I deployed this code, the website crashes on startup. I am very confident that it is an issue with process.env.PWD, so I am wondering what the proper method of getting Meteor's root directory on a deployed app is.

    //code to run on server at startup
    var path = Npm.require('path')

    //I also tried using the below line (which was recommended in another Stackoverflow question) to no avail
    //var meteor_root = Npm.require('fs').realpathSync( process.cwd() + '/../' );

    var apnagent = Meteor.require("apnagent"),
    agent = new apnagent.Agent();
    agent.set('cert file', path.join(process.env.PWD, "private", "certificate-file.pem"))
    agent.set('key file', path.join(process.env.PWD, "private", "devkey-file.pem"))
Was it helpful?

Solution

In development mode the file structure is different than after bundling, so you should never rely on it. Particularly, you should not access your files directly like you're doing with path methods.

Loading private assets is described in this section of Meteor's documentation. It mostly boils down to this method:

Assets.getBinary("certificate-file.pem");

and it's getText counterpart.

As for configuring APN agent, see this section of documentation. You don't have to configure the agent by passing file path as cert file param. Instead you may pass the raw data returned by Assets methods directly as cert. The same holds for key file ~ key pair and other settings.

As an alternative, you would need to submit your files independently to the production server to a different folder than your Meteor app and use their global path. This, however, would not be possible for cloud providers like Heroku, so it's better to use assets in the intended way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top