var dummy = require('./routes/dummy.js');
var showdata = require('./routes/showdata.js');
var somemore = require('./routes/somemore.js');
var app = express();
app.use('/dummy', dummy);
app.use('/showdata', showdata);
app.use('/somemore', somemore);
DUMMY.JS file:
var express = require('express');
var router = express.Router();
module.exports = function()
{
var data = 'dummy.js';
console.log('Im a programer! - look it will show up!);
return router;
};
//or: module.exports = {data : 'dummy.js', router : router};
SHOWDATA.JS file:
var express = require('express');
var router = express.Router();
var readData = require(./dummy.js);
router.get('/', function(req, res, next)
{
res.render('showdata');
});
OK, now when I type console.log(readData().data); in showdata.js file I can’t access var data = ‘dummy.js’; (undefined). Cause it’s local variable and not a property… right ( console.log() is working fine )? But is there any way to get to it - display it in showdata.js file? Even when I use module.exports as object - module.exports = {data : ‘dummy.js’, router : router}; I get “Router.use() requires middleware function but got a Object”… Thanks for pointing any directions.
BUT everything I want to export is in module.exports so explain it to me how it disturb the scope? As I understand module.exports object works like ability to export everything ->inside it<-. Console.log is working fine and I can display it wherever I want. When I change var precious_data to precious_data it should be available everywhere as a global data but it’s not in this particular case.
When I use object in module.exports:
var func = function() {console.log('Im a programer from function!');}
var read = 'text to read';
module.exports = {data : 'dummy.js', func : func, read: read, router : router};
everything is working fine (even when some of those are not global vars)…
var readData = require(./dummy.js).data; // or require(./dummy.js).func - whatever;
console.log(readData); // all good
well I needed to disable app.use(‘/dummy’, dummy); cause of Router.use() requires middleware function but got a Object… why it must be a function… why it conflict with module.exports = router;… few lines on this subject won’t hurt my brain either
Can you elaborate more on this topic? Help me understand this basic concept.
neither of them are global variables. that is the trick with module.exports, it keeps its scope to itself and ony publishes the desired stuff. Like in the (revealing) module pattern.