在node.js中加载并执行外部js文件,可以访问局部变量吗? [英] Load and execute external js file in node.js with access to local variables?
问题描述
在node.js中执行简单的 include('./ path / to / file')
命令类型是否容易/可行?
Is it easy/possible to do a simple include('./path/to/file')
type of command in node.js?
我想要做的就是访问本地变量并运行脚本。人们通常如何组织比简单的hello世界更大的node.js项目? (功能齐全的动态网站)
All I want to do is have access to local variables and run a script. How do people typically organize node.js projects that are bigger than a simple hello world? (A fully functional dynamic website)
例如,我希望有以下目录:
For example I'd like to have directories like:
/ models
/ views
...等
推荐答案
只需做一个 require('。 /yourfile.js');
将您想要外部访问的所有变量声明为全局变量。
所以而不是
Declare all the variables that you want outside access as global variables. So instead of
var a =hello
它将是
GLOBAL.a =hello
或只是
a =你好
这显然很糟糕。您不希望污染全球范围。
相反,建议方法是 export
你的函数/变量。
This is obviously bad. You don't want to be polluting the global scope.
Instead the suggest method is to export
your functions/variables.
如果你想要MVC模式看看Geddy。
If you want the MVC pattern take a look at Geddy.
这篇关于在node.js中加载并执行外部js文件,可以访问局部变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!