在带有 express JS 的 Node 应用程序中,如何包含和 运行 一个单独的 JS 文件?
In a Node app with express JS, how can I include and run a separate JS file?
我有一个 javascript 文件,其中包含一些获取 RSS 提要并将内容保存在数据库中的函数。我最初让 HTML 页面调用它,但我希望这个文件一直 运行ning 在后端(从 RSS 提要中获取更新并将其保存到数据库中) ).
我的问题是,如何在我的应用程序中附加和 运行 这个单独的 javascript?我假设它看起来像这样:
在app.js中:
var RSSReader = require('./public/javascripts/RSSReader.js');
RSSReader.SomeFunction();
虽然这不起作用。另外,我的 app.js 中声明的变量是否可以在 RSSReader.js 中使用?
谢谢。
how can I attach and run this separate javascript within my app?
您显示的 app.js 代码应该可以正常工作。关键是你必须把你的 RSSReader.js 文件变成一个导出它想要的功能的模块 public:
所以,在 RSSReader.js 中,你会得到这样的东西:
module.exports = {
someFunction: function() {
// code here
},
someOtherFunction: function() {
// code here
}
};
然后,在您的其他文件中,您可以加载该模块并像以前一样使用它:
var RSSReader = require('./public/javascripts/RSSReader.js');
RSSReader.someFunction();
RssReader.someOtherFunction();
node.js 模块的文档是 here.
Also, would variables declared in my app.js be available in
RSSReader.js?
不,除非您将 app.js 变量显式声明为 global
对象的属性,否则它们不会。从 app.js 共享到另一个模块的通常 node.js 约定是为 RSSReader.js 模块创建一个初始化方法(您可以将其称为 init
),然后将其传递给任何它需要的上下文(通常是一个带有某些属性的对象)来自 app.js,然后 RSSReader.js 模块可以存储该上下文以供其使用。
因此,如果您想共享从 app.js 到 RSSReader.js 的一些变量,您可以通过 .init()
方法共享它们,如下所示:
RSSReader.js
var data;
module.exports = {
init: function(options) {
data = options;
},
someFunction: function() {
// code here can access the data variable
},
someOtherFunction: function() {
// code here can access the data variable
}
};
app.js
var RSSReader = require('./public/javascripts/RSSReader.js');
RSSReader.init({express: express, db: db});
RSSReader.someFunction();
RssReader.someOtherFunction();
我有一个 javascript 文件,其中包含一些获取 RSS 提要并将内容保存在数据库中的函数。我最初让 HTML 页面调用它,但我希望这个文件一直 运行ning 在后端(从 RSS 提要中获取更新并将其保存到数据库中) ).
我的问题是,如何在我的应用程序中附加和 运行 这个单独的 javascript?我假设它看起来像这样:
在app.js中:
var RSSReader = require('./public/javascripts/RSSReader.js');
RSSReader.SomeFunction();
虽然这不起作用。另外,我的 app.js 中声明的变量是否可以在 RSSReader.js 中使用?
谢谢。
how can I attach and run this separate javascript within my app?
您显示的 app.js 代码应该可以正常工作。关键是你必须把你的 RSSReader.js 文件变成一个导出它想要的功能的模块 public:
所以,在 RSSReader.js 中,你会得到这样的东西:
module.exports = {
someFunction: function() {
// code here
},
someOtherFunction: function() {
// code here
}
};
然后,在您的其他文件中,您可以加载该模块并像以前一样使用它:
var RSSReader = require('./public/javascripts/RSSReader.js');
RSSReader.someFunction();
RssReader.someOtherFunction();
node.js 模块的文档是 here.
Also, would variables declared in my app.js be available in RSSReader.js?
不,除非您将 app.js 变量显式声明为 global
对象的属性,否则它们不会。从 app.js 共享到另一个模块的通常 node.js 约定是为 RSSReader.js 模块创建一个初始化方法(您可以将其称为 init
),然后将其传递给任何它需要的上下文(通常是一个带有某些属性的对象)来自 app.js,然后 RSSReader.js 模块可以存储该上下文以供其使用。
因此,如果您想共享从 app.js 到 RSSReader.js 的一些变量,您可以通过 .init()
方法共享它们,如下所示:
RSSReader.js
var data;
module.exports = {
init: function(options) {
data = options;
},
someFunction: function() {
// code here can access the data variable
},
someOtherFunction: function() {
// code here can access the data variable
}
};
app.js
var RSSReader = require('./public/javascripts/RSSReader.js');
RSSReader.init({express: express, db: db});
RSSReader.someFunction();
RssReader.someOtherFunction();