我可以在Microsoft Azure的Node.JS中使用本机"fs"文件系统吗? [英] Can I use the native 'fs' filesystem in Node.JS on Microsoft Azure?
问题描述
我已经将一些功能不错的nodeJS代码部署到了Azure.它使用Jade视图引擎,通过fs文件库拉入模板:
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);
});
}
它在我的Mac上本地工作正常,但是从Azure部署收到时,呈现的电子邮件模板显示为< undefined></undefined>
.恐怕Azure上有些时髦,我无法使用'fs'加载文件,而可能不得不使用Blob.
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.
这是Azure中的限制吗?还是该问题与该fs.ReadFile方法无关?
Is this a limitation in Azure? Or could the problem be unrelated to this fs.ReadFile method?
作为健全性检查,我将此部署到Nodejitsu上,并且运行良好.
As a sanity check, I deployed this up to Nodejitsu and it works fine.
非常感谢!
推荐答案
这正是我使用azure和Node.JS遇到的问题.在挖掘许多节点变量后,我发现的解决方案是使用__file.
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 始终返回目录名称当前文件.如果您在根文件夹中的文件中使用此值定义变量,则将具有应用程序基本文件夹.我已经实现了该功能,并且在azure和heroku中可以正常工作.
__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.
这篇关于我可以在Microsoft Azure的Node.JS中使用本机"fs"文件系统吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!