【问题标题】:Using require in nodejs to access external javascript files在 nodejs 中使用 require 访问外部 javascript 文件
【发布时间】:2015-03-18 13:01:48
【问题描述】:

我一直在尝试在 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 [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\route.js:110: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);
        // });

});

【问题讨论】:

    标签: javascript node.js file require


    【解决方案1】:

    只需编写函数并将其导出到对象中(不带 .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
    

    【讨论】:

    • 我添加了你提到的内容,它在需要时不再崩溃,尽管现在它没有超出那条线。意思是,它永远不会到达 console.log("here?");我在它之后添加了。
    • 能否请您发布您希望包含其他模块的整个主文件
    • 我添加了方法,整个文件是一个相当大的restful api
    • 我在执行 GET 调用时犯了一个错误(已激活 POST,因此它没有收到错误的原因),但即使您进行了修改,我仍然会收到此错误,如编辑 3 所示
    • 从哪里可以得到name的值?
    【解决方案2】:

    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
    

    【讨论】:

    • 这给了我同样的错误,我已经编辑了我的问题以包含给出的错误,以及我的确切代码,我之前以简化的方式发布了一个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多