繁体   English   中英

在Node.js中使用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收到错误:


在Function.Module._resolveFilename(module.js:338:15)
在Function.Module._load(module.js:280:25)
在Module.require(module.js:364:17)
在需要时(module.js:380:17)
在server.use.express.static.index(D:\\ udu \\ tcide_a \\ src \\ tcide \\ server.js:613:24)
在Layer.handle上[作为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 \\ route.js:110:13)
在Route.dispatch(D:\\ udu \\ tcide_a \\ src \\ tcide \\ node_modules \\ express \\ lib \\ router \\ route.js:91:3)
在Layer.handle上[作为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM