在NodeJS模块中导出函数

时间:2022-05-11 16:56:14

I have a module in NodeJS which has the following definition:

我在NodeJS中有一个模块,它有以下定义:

var express = require('express');
var router = express.Router();

function myFunction(){
    //do some stuff
};

router.get('/url', function(req, res, callback) {   
    var data = myFunction();
    res.render('index', {
        item: data
    });
});

module.exports = router;

I want it to be called in both ways:

我希望它能以两种方式被调用:

HTTP petition:

HTTP请愿书:

http://localhost:3090/url

As a function in another module:

作为另一个模块的功能:

var myModule = require('myModule');
var data = myModule.myFunction();

I can access the module by HTTP in the way shown above. However, I don't know how to export myFunction to be used in another module. I have tried the following without any success:

我可以通过HTTP以上面显示的方式访问模块。但是,我不知道如何导出myFunction以在另一个模块中使用。我试过以下但没有成功:

router.myFunction = myFunction;
module.exports = router;

And:

和:

module.exports = router;
module.exports.myFunction = myFunction;

How could I solve this problemn? Thank you very much in advance

我怎么能解决这个问题呢?非常感谢你提前

2 个解决方案

#1


2  

you can make these changes

你可以做出这些改变

use exports to expose multiple functions

使用exports来公开多个函数

exports.router = router; 
exports.myFunction = myFunction;

for including them both in other file(path to myModule can vary as per your structure) you can now include them as

如果将它们都包含在其他文件中(myModule的路径可能因您的结构而异),您现在可以将它们包括在内

var routes= require('./myModule').router;
var myfunction = require('./myModule').myFunction;

#2


1  

Another way is to also sum up everything you're exporting at the end of the module - basically construct the exports object:

另一种方法是总结在模块末尾导出的所有内容 - 基本上构造导出对象:

module.exports = {

    myFunction: myFunction,
    router: router,
    someConstant: 42,
    anotherValue: calculateThisValue()
}

At any time, module.exports is a global per-file object you get when you require that file. If you put nothing there, it'll be undefined. If you make it a function, then it'll be a function. If you make an object literal like above, you get an object. You can also export primitives, like dates, arrays, or whatever else you may think of.

在任何时候,module.exports都是您需要该文件时获得的全局每文件对象。如果你什么都没有,它将是未定义的。如果你把它变成一个函数,那么它就是一个函数。如果你像上面那样创建一个对象文字,你会得到一个对象。您还可以导出基元,如日期,数组或您可能想到的任何其他内容。

#1


2  

you can make these changes

你可以做出这些改变

use exports to expose multiple functions

使用exports来公开多个函数

exports.router = router; 
exports.myFunction = myFunction;

for including them both in other file(path to myModule can vary as per your structure) you can now include them as

如果将它们都包含在其他文件中(myModule的路径可能因您的结构而异),您现在可以将它们包括在内

var routes= require('./myModule').router;
var myfunction = require('./myModule').myFunction;

#2


1  

Another way is to also sum up everything you're exporting at the end of the module - basically construct the exports object:

另一种方法是总结在模块末尾导出的所有内容 - 基本上构造导出对象:

module.exports = {

    myFunction: myFunction,
    router: router,
    someConstant: 42,
    anotherValue: calculateThisValue()
}

At any time, module.exports is a global per-file object you get when you require that file. If you put nothing there, it'll be undefined. If you make it a function, then it'll be a function. If you make an object literal like above, you get an object. You can also export primitives, like dates, arrays, or whatever else you may think of.

在任何时候,module.exports都是您需要该文件时获得的全局每文件对象。如果你什么都没有,它将是未定义的。如果你把它变成一个函数,那么它就是一个函数。如果你像上面那样创建一个对象文字,你会得到一个对象。您还可以导出基元,如日期,数组或您可能想到的任何其他内容。