Dynamically insert files into meteor public folder without hiding it

meteor

Solution

The accepted answer did not work for me, but as of version 0.6.6.3 you can do the following:

var fs = Npm.require('fs');
WebApp.connectHandlers.use(function(req, res, next) {
    var re = /^\/url_path\/(.*)$/.exec(req.url);
    if (re !== null) {   // Only handle URLs that start with /url_path/*
        var filePath = process.env.PWD + '/.server_path/' + re[1];
        var data = fs.readFileSync(filePath, data);
        res.writeHead(200, {
                'Content-Type': 'image'
            });
        res.write(data);
        res.end();
    } else {  // Other urls will have default behaviors
        next();
    }
});

Notes

- `process.env.PWD` will give you the project root

if you plan to put files inside your project

- don't use the `public` or `private` meteor folders

- use dot folders (eg. hidden folders ex: `.uploads`)

Not respecting these two will cause local meteor to restart on every upload, unless you run your meteor app with: `meteor run --production`

Problem

I have a meteor application that generates images. After they are generated, I want to serve them. But each time I write to the public folder, my meteor server restarts. I searched for a solution and found several workarounds: Serve files outside of the project folder - At the moment I don't know how to achieve this, would I have to write some kind of middleware that integrates into meteor? Add a tilde ~ to the folder in `public/` - which seems to make meteor ignore the folder altogether, when trying to access files in the folder I get redirected to my root page. Run meteor in production mode. Seems like a dirty workaround for me. Right now, `meteor run --production` still restarts my server so I have to bundle my app, reinstall fibers every time, set my environment variables and then run the app. Every time I change something. Are there any other solutions out there?

Original source