Is it possible to use closure as inter function dto between two files?
This is my test code, the idea I am trying to work out, and it might as well be false, because I am coming from Java land and the dto way of thinking is doing well in me, is to create closure (store.js) in my app file (index.js), have that closure store some state, and then load this closure in another file (router.js).
If this is wrong is there a better way to do it? My aim is to access Request object by reference from multiple files, rather than possibly passing it around as a function argument.
store.js
store = function(request) {
let req = request
function getRequest(){
return req
}
return getRequest
}
module.exports = store;
index.js
var express = require('express');
var app = express();
var router = require('./router');
var store = require('./store')
app.use(function(request, response, next){
require('./store')(request)
next()
})
app.use('/', router);
app.listen(3001);
router.js
var express = require('express');
var router = express.Router();
router.get('/:id', function(req, res){
const store = require('./store');
res.send('store = ' + store());
});
module.exports = router;