TIM*_*MEX 893 javascript import header node.js
假设我有一个名为app.js的文件.很简单:
var express = require('express');
var app = express.createServer();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(req, res){
res.render('index', {locals: {
title: 'NowJS + Express Example'
}});
});
app.listen(8080);
Run Code Online (Sandbox Code Playgroud)
如果我在"tools.js"中有一个函数怎么办?如何导入它们以在apps.js中使用?
或者......我应该将"工具"变成一个模块,然后需要它吗?<<似乎很难,我宁愿做tools.js文件的基本导入.
mas*_*lum 1307
您可以要求任何js文件,您只需要声明要公开的内容.
// tools.js
// ========
module.exports = {
foo: function () {
// whatever
},
bar: function () {
// whatever
}
};
var zemba = function () {
}
Run Code Online (Sandbox Code Playgroud)
在您的app文件中:
// app.js
// ======
var tools = require('./tools');
console.log(typeof tools.foo); // => 'function'
console.log(typeof tools.bar); // => 'function'
console.log(typeof tools.zemba); // => undefined
Run Code Online (Sandbox Code Playgroud)
Udo*_*o G 290
如果尽管有其他所有答案,您仍然希望传统上在node.js源文件中包含一个文件,您可以使用:
var fs = require('fs');
// file is included here:
eval(fs.readFileSync('tools.js')+'');
Run Code Online (Sandbox Code Playgroud)
+''是将文件内容作为字符串而不是对象获取所必需的(.toString()如果您愿意,也可以使用).include()实用函数或类似的东西).请注意,在大多数情况下,这是不好的做法,您应该编写一个模块.但是,在极少数情况下,您本地上下文/命名空间的污染是您真正想要的.
另请注意,这不适用"use strict";(当您处于"严格模式"时),因为执行导入的代码无法访问 "导入"文件中定义的函数和变量.严格模式强制执行由较新版本的语言标准定义的某些规则.这可能是避免此处描述的解决方案的另一个原因.
小智 178
您不需要新功能或新模块.如果您不想使用命名空间,则只需执行您正在调用的模块.
module.exports = function() {
this.sum = function(a,b) { return a+b };
this.multiply = function(a,b) { return a*b };
//etc
}
Run Code Online (Sandbox Code Playgroud)
或者像myController.js这样的任何其他.js:
代替
var tools = require('tools.js') 这迫使我们使用命名空间和调用工具 tools.sum(1,2);
我们可以简单地打电话
require('tools.js')();
Run Code Online (Sandbox Code Playgroud)
然后
sum(1,2);
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我有一个控制器ctrls.js的文件
module.exports = function() {
this.Categories = require('categories.js');
}
Run Code Online (Sandbox Code Playgroud)
我可以Categories在每个背景下用作公共课require('ctrls.js')()
小智 105
创建两个js文件
// File cal.js
module.exports = {
sum: function(a,b) {
return a+b
},
multiply: function(a,b) {
return a*b
}
};
Run Code Online (Sandbox Code Playgroud)
主要的js文件
// File app.js
var tools = require("./cal.js");
var value = tools.sum(10,20);
console.log("Value: "+value);
Run Code Online (Sandbox Code Playgroud)
产量
Value: 30
Run Code Online (Sandbox Code Playgroud)
Pla*_*der 39
这是一个简单明了的解释:
// Include the public functions from 'helpers.js'
var helpers = require('./helpers');
// Let's assume this is the data which comes from the database or somewhere else
var databaseName = 'Walter';
var databaseSurname = 'Heisenberg';
// Use the function from 'helpers.js' in the main file, which is server.js
var fullname = helpers.concatenateNames(databaseName, databaseSurname);
Run Code Online (Sandbox Code Playgroud)
// 'module.exports' is a node.JS specific feature, it does not work with regular JavaScript
module.exports =
{
// This is the function which will be called in the main file, which is server.js
// The parameters 'name' and 'surname' will be provided inside the function
// when the function is called in the main file.
// Example: concatenameNames('John,'Doe');
concatenateNames: function (name, surname)
{
var wholeName = name + " " + surname;
return wholeName;
},
sampleFunctionTwo: function ()
{
}
};
// Private variables and functions which will not be accessible outside this file
var privateFunction = function ()
{
};
Run Code Online (Sandbox Code Playgroud)
小智 33
我也在寻找NodeJS'include'功能,我检查了Udo G提出的解决方案- 请参阅消息/sf/answers/612116361/.他的代码不适用于我包含的JS文件.最后我解决了这个问题:
var fs = require("fs");
function read(f) {
return fs.readFileSync(f).toString();
}
function include(f) {
eval.apply(global, [read(f)]);
}
include('somefile_with_some_declarations.js');
Run Code Online (Sandbox Code Playgroud)
当然,这有帮助.
Dha*_*esh 26
说我们要调用函数平()和添加(30,20),这是在lib.js从文件main.js
main.js
lib = require("./lib.js")
output = lib.ping();
console.log(output);
//Passing Parameters
console.log("Sum of A and B = " + lib.add(20,30))
Run Code Online (Sandbox Code Playgroud)
lib.js
this.ping=function ()
{
return "Ping Success"
}
Run Code Online (Sandbox Code Playgroud)
//Functions with parameters
this.add=function(a,b)
{
return a+b
}
Run Code Online (Sandbox Code Playgroud)
Ing*_*ngo 26
创建两个 JavaScript 文件。例如import_functions.js和main.js
1.) import_functions.js
// Declaration --------------------------------------
module.exports =
{
add,
subtract
// ...
}
// Implementation ----------------------------------
function add(x, y)
{
return x + y;
}
function subtract(x, y)
{
return x - y;
}
// ...
Run Code Online (Sandbox Code Playgroud)
2.) main.js
// include ---------------------------------------
const sf= require("./import_functions.js")
// use -------------------------------------------
var x = sf.add(4,2);
console.log(x);
var y = sf.subtract(4,2);
console.log(y);
Run Code Online (Sandbox Code Playgroud)
输出
6
2
Run Code Online (Sandbox Code Playgroud)
小智 25
Node.js中的vm模块提供了在当前上下文(包括全局对象)中执行JavaScript代码的能力.见http://nodejs.org/docs/latest/api/vm.html#vm_vm_runinthiscontext_code_filename
请注意,截至今天,vm模块中存在一个错误,该错误阻止runInThisContext在从新上下文调用时执行此操作.这只有在主程序在新上下文中执行代码然后该代码调用runInThisContext时才有意义.请参阅https://github.com/joyent/node/issues/898
遗憾的是,Fernando建议的with(全局)方法不适用于像"function foo(){}"这样的命名函数
简而言之,这是一个适合我的include()函数:
function include(path) {
var code = fs.readFileSync(path, 'utf-8');
vm.runInThisContext(code, path);
}
Run Code Online (Sandbox Code Playgroud)
小智 13
Udo G.说:
- eval()不能在函数内部使用,必须在全局范围内调用,否则不能访问任何函数或变量(即不能创建include()实用程序函数或类似的东西).
他是对的,但有一种方法可以影响一个函数的全局范围.改善他的榜样:
function include(file_) {
with (global) {
eval(fs.readFileSync(file_) + '');
};
};
include('somefile_with_some_declarations.js');
// the declarations are now accessible here.
Run Code Online (Sandbox Code Playgroud)
希望,这有帮助.
Jim*_*dra 11
您可以将函数放在全局变量中,但最好将工具脚本转换为模块.这真的不太难 - 只需将您的公共API附加到exports对象上即可.看看了解Node.js的出口模块的一些细节.
Ale*_*nho 11
在我看来,另一种方法是使用(function(/*things here*/){})();来调用require()函数时执行lib文件中的所有内容.这样做会使所有这些函数成为全局范围,就像eval()解决方案一样
SRC/lib.js
(function () {
funcOne = function() {
console.log('mlt funcOne here');
}
funcThree = function(firstName) {
console.log(firstName, 'calls funcThree here');
}
name = "Mulatinho";
myobject = {
title: 'Node.JS is cool',
funcFour: function() {
return console.log('internal funcFour() called here');
}
}
})();
Run Code Online (Sandbox Code Playgroud)
然后在您的主代码中,您可以通过以下名称调用您的函数:
main.js
require('./src/lib')
funcOne();
funcThree('Alex');
console.log(name);
console.log(myobject);
console.log(myobject.funcFour());
Run Code Online (Sandbox Code Playgroud)
会产生这个输出
bash-3.2$ node -v
v7.2.1
bash-3.2$ node main.js
mlt funcOne here
Alex calls funcThree here
Mulatinho
{ title: 'Node.JS is cool', funcFour: [Function: funcFour] }
internal funcFour() called here
undefined
Run Code Online (Sandbox Code Playgroud)
支付atention的不确定时,你叫我object.funcFour() ,这将是相同的,如果你加载的eval() .希望能帮助到你 :)
M.H*_*fny 10
它跟我一样,如下......
Lib1.js
//Any other private code here
// Code you want to export
exports.function1 = function(params) {.......};
exports.function2 = function(params) {.......};
// Again any private code
Run Code Online (Sandbox Code Playgroud)
现在在Main.js文件中你需要包含Lib1.js
var mylib = requires('lib1.js');
mylib.function1(params);
mylib.function2(params);
Run Code Online (Sandbox Code Playgroud)
请记住将Lib1.js放在node_modules文件夹中.
我只想添加,如果您只需要从tools.js导入某些函数,那么您可以使用自6.4版以来node.js中支持的解构赋值 - 请参阅node.green.
示例 :( 两个文件都在同一个文件夹中)
tools.js
module.exports = {
sum: function(a,b) {
return a + b;
},
isEven: function(a) {
return a % 2 == 0;
}
};
Run Code Online (Sandbox Code Playgroud)
main.js
const { isEven } = require('./tools.js');
console.log(isEven(10));
Run Code Online (Sandbox Code Playgroud)
输出: true
这也避免了您将这些函数指定为另一个对象的属性,如下面(常见)赋值中的情况:
const tools = require('./tools.js');
你需要打电话的地方tools.isEven(10).
注意:
不要忘记在文件名前加上正确的路径 - 即使两个文件都在同一个文件夹中,也需要加前缀 ./
来自Node.js文档:
如果没有前导'/','./'或'../'来表示文件,则模块必须是核心模块或从node_modules文件夹加载.
app.js
let { func_name } = require('path_to_tools.js');
func_name(); //function calling
Run Code Online (Sandbox Code Playgroud)
tools.js
let func_name = function() {
...
//function body
...
};
module.exports = { func_name };
Run Code Online (Sandbox Code Playgroud)
创建两个文件,例如app.js和main.js
app.js
const mainFile= require("./main.js")
var x = mainFile.add(2,4) ;
console.log(x);
Run Code Online (Sandbox Code Playgroud)
main.js
const add = function(x, y){
return x+y;
}
module.exports ={
add:add
}
Run Code Online (Sandbox Code Playgroud)
输出
6
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
694159 次 |
| 最近记录: |