I have a module in NodeJS which has the following definition:
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://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:
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
module.exports.router = router; module.exports.myFunction = myFunction;? sounds more like you'd want to split the two in two seperate modules though