在nodejs中使用require来访问外部javascript文件
Using require in nodejs to access external javascript files
我一直在尝试在nodejs中使用require来访问带有简单脚本的javascript文件。我的目标是要求脚本并将其返回值输出到控制台。到目前为止,这就是我所拥有的:
oper2 = function(){
var x = 1;
var y = x+1;
return y;
};
module.exports = {
oper2: oper2
};
这是我想通过我的nodejs api访问的文件,在我尝试使用的api中有以下代码require:
var dir = 'files/'+name;
var func = require('./'+dir);
console.log("here?");
console.log("Value" + func.oper2()); //test operation
res.json('done');//value received by the api
我已经尝试了该文件的多个版本和代码来要求该文件,但还没有走多远。它总是在var func=…中崩溃。。。。。行中表示错误找不到模块。尽管在此之前我有代码来测试它是否读取文件,并且它正在查找和读取文件。
如有任何帮助,我们将不胜感激,谢谢。
编辑:
files.route('/:name/execute')
.post(function (req, res){
var dir = 'files/'+name;
var func = require('./'+dir);
console.log("here?");
console.log("Value " + func.oper2);
res.json('done');
});
目前,它在需要时仍然会崩溃。
正在接收编辑3错误:
nbsp;在Function。模块_resolveFilename(module.js:338:15)
在Function。模块_加载(module.js:280:25)
在Module.require(Module.js:364:17)
at required(模块.js:380:17)
位于server.use.express.static.index(D:''udu''tcide_a''src''tcide''server.js:613:24)
在Layer.handle[as-handle_request](D:''udu''tcide_a''src''tcide''node_modules''express''lib''router''Layer.js:82:5)
在next(D:''udu''tcide_a''src''tcide''node_modules''express''lib''router''route.js:11:13)
在Route.dispatch(D:''udu''tcide_a''src''tcide''node_modules''express''lib''router''Route.js:91:3)
在Layer.handle[as-handle_request](D:''udu''tcide_a''src''tcide''node_modules''express''lib''router''Layer.js:82:5)
位于D:''udu''tcide_a''src''tcide''node_modules''express''lib''router''index.js:267:22
编辑4(这是我的确切代码,我只是在上面简化了它):
behaviorR.route('/:behavior/execute')
.get(function (req, res){
var dir = 'operations/'+req.params.behavior;
// if(!fs.existsSync(dir)){
// console.log('Not found');;
// }
// else{
console.log('About to require');
var func = require('./'+dir);
console.log("here?");
// console.log("Value %d", func.oper2(5));
res.json('done');
// }
// fs.readFile('operations/'+req.params.behavior+'.js', "utf8", function(error, data) {
// res.json(data);
// });
});
只需编写函数并将其导出到对象(不带.js)中
var oper2 = function(value){
var x = 1;
var y = x+1+value;
return y;
};
// Export object which contains the above method
module.exports = {
oper2: oper2
};
在主模块中
var dir = 'files/'+name;
var func = require('./'+dir);
console.log("Value %d", func.oper2(5)); //test operation
res.json('done');//value received by the api
module.exports
是require返回的对象。使用module.exports将允许像函数一样调用所需的模块。
var oper2 = function(value){
var x = 1;
var y = x+1+value;
return y;
};
module.exports = oper2;
并且在主文件中
var dir = 'files/'+name;
var func = require('./'+dir);
console.log("Value %d", func(5)); //test operation
res.json('done');//value received by the api
使用exports
,您可以将函数和对象添加到模块的根目录中。
exports.oper2 = function(value){
var x = 1;
var y = x+1+value;
return y;
};
并且在主文件中
var dir = 'files/'+name;
var func = require('./'+dir);
console.log("Value %d", func.oper2(5)); //test operation
res.json('done');//value received by the api
- Javascript从匿名函数访问外部对象属性
- 在nodejs中使用require来访问外部javascript文件
- TypeScript:在对象文字字段中的访问器中访问外部“this”
- jQuery:如何访问外部变量
- 从jQuery.ech()函数访问外部作用域
- 我可以't访问外部javascript文件中的freemarker变量
- 是否有任何方法可以访问外部范围's变量
- 访问外部调用方函数参数
- 即使在引用后也无法访问外部Jscript文件
- JQuery 承诺:访问外部变量或将结果传递给 .done 内部的 IIFE
- 从内联 html 页面访问外部 JS 变量
- 咕噜声:访问外部文件中的 YAML 符号
- 从 angularjs 控制器访问外部 JavaScript 函数和类
- Chrome 应用程序使用 JavaScript / jQuery / AngularJS 访问外部资源
- 使用 JavaScript 和/或 jQuery 访问外部 HTML 正文的宽度和高度
- 评估是否可以访问外部函数
- 如何从传入的预定义回调访问外部变量
- 为什么我无法使用codeigniter访问外部javascript文件,即使它们被加载了
- 从 js obj 数组访问外部 js obj 属性
- 如何访问外部JavaScript文件中的DOM对象