Hairgami_Master
Hairgami_Master

Reputation: 5539

Can I use the native 'fs' filesystem in Node.JS on Microsoft Azure?

I've deployed a functioning bit of nodeJS code to Azure. It uses the Jade view engine, pulling in template via the fs file library:

function generateEmail(emailType, params, callback){
fs.readFile(__parentDir + '/emailTemplates/' + emailType + '.jade', 'utf8', function (err, data) {
    var fn = jade.compile(data);
    var body = fn({data: params, moment: moment});    //The data going in to the template
    callback(body);
});

}

It works fine locally on my Mac, but the rendered email templates show up as <undefined></undefined> when received from the Azure deployment. I'm afraid there's something funky on Azure where I can't use 'fs' to load files and might have to use Blobs instead.

Is this a limitation in Azure? Or could the problem be unrelated to this fs.ReadFile method?

As a sanity check, I deployed this up to Nodejitsu and it works fine.

Many thanks!

Upvotes: 2

Views: 2156

Answers (1)

guillem
guillem

Reputation: 2998

This was exactly the problem I had with azure and Node.JS. The solution I found after digging with many node variables was to use __file.

As a quick way to test this you can add to your app.js something like :

// global
baseDir = __dirname;

Then you would have this global variable accessible from anywhere. ie inside the router folder.

var defaultFile = path.join(baseDir, 'sample.txt');

__dirname always return the directory name for the current file. If you define a variable with this value from a file in the root folder you will have the application base folder.I have implemented this and it's working i azure and heroku.

Upvotes: 3

Related Questions