问题描述
在我的本地计算机上,此代码:
On my local machine, this code:
var path = require('path');
var appdir = path.dirname(require.main.filename);
console.log(appdir);
将记录"c:\ sites \ mysite"
will log "c:\sites\mysite"
在Azure上,我看到的是:D:\ Program Files(x86)\ iisnode
On Azure, I see this instead: D:\Program Files (x86)\iisnode
我从设置中知道属于我自己的节点版本,我的应用程序的路径为D:\ home \ site \ wwwroot
I know, from setting the node version to be my own that the path to my app is D:\home\site\wwwroot
并且,根据 docs ,当前应用程序的入口点可以通过检查require.main.filename."
And, according to the docs "the entry point of the current application can be obtained by checking require.main.filename."
关于可能出什么问题的任何想法?
Any ideas on what could be going wrong?
推荐答案
在本地计算机上运行应用程序时,您可能会执行以下命令
When you run your application on your local machine you probably execute following command
node.exe your_application.js
这就是为什么 require.main.filename
返回的原因您的应用程序的入口点.
That is why require.main.filename
returns the entry point of your application.
请注意,将应用程序部署到Windows Azure网站时,请使用 iisnode -本机IIS 7/8模块,允许在IIS 7/8中托管Node.js应用程序.这就是为什么 require.main.filename
在Windows Azure上返回 D:\ Program Files(x86)\ iisnode
的原因.
Please note that when you deploy your application to Windows Azure WebSite you use iisnode - a native IIS 7/8 module that allows hosting of Node.js applications in IIS 7/8. That is why require.main.filename
returns D:\Program Files (x86)\iisnode
on Windows Azure.
我建议使用 __ dirname
模块局部变量(通常与 path.join()
结合使用,例如var mydir = path.join(__ dirname,'..')
)以获得所需的目录.
I would recommend using __dirname
module local variable (usually combined with path.join()
e.g. var mydir = path.join(__dirname, '..')
) in order to obtain required directory.
我希望这能解释您的问题.
I hope that explains your question.
这篇关于为什么Azure上的require.main.filename与我的本地Windows计算机上的不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!