I have an issue when requiring Express.js middleware; I have the code in app.'s
:
...
var middlewares = require('./middlewares');
...
routes = require('./routes')(app, config, crypto, middlewares, models, oauth2);
...
This requires ./middlewares/index.js
:
/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/
'use strict';
var middlewares = function (app, config, models, oauth2) {
var that = {};
that.touch = require('./touch.js')(app, config, models, oauth2);
return that;
};
module.exports = middlewares;
Which in turn (the above is simplified but would contain require other middleware too) requires ./middlewares/touch.js
:
/*jslint es5: true, indent: 2, node: true, nomen: true, maxlen: 80, vars: true*/
'use strict';
var touch = function (app, config, models, oauth2) {
return function (req, res, next) {
console.log('Touch');
next();
};
};
module.exports = touch;
And call it from ./routes/index.js
:
/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/
'use strict';
var routes = function (app, config, crypto, middlewares, models, oauth2) {
var that = {};
app.get(
'/',
middlewares.touch(app, config, models, oauth2),
function (req, res) {
res.send('Hello world!');
}
);
that.auth = require('./auth')(app, config, crypto, models, oauth2);
return that;
};
module.exports = routes;
Though when I do this I get the error:
TypeError: Object function (app, config, models, oauth2) {
var that = {};
that.touch = require('./touch.js')(app, config, models, oauth2);
return that;
} has no method 'touch'
When I require ./middlewares/touch.js
from app.js
like:
...
var touch = require('./middlewares/touch.js');
...
routes = require('./routes')(app, config, crypto, touch, models, oauth2);
...
And in ./routes/index.js
I call it like:
/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/
'use strict';
var routes = function (app, config, crypto, touch, models, oauth2) {
var that = {};
app.get(
'/',
touch(app, config, models, oauth2),
function (req, res) {
res.send('Hello world!');
}
);
that.auth = require('./auth')(app, config, crypto, models, oauth2);
return that;
};
module.exports = routes;
I am error free! Must be the way I am referencing it through ./middlewares/index.js
but I can for the life of me work it out?
Any ideas Stack Overflow?
EDIT:
OK, so then the question remains;
- I want to
require
./middlewares/index.js
from inside app.js
./middlewares/index.js
requires all my middlewares e.g. ./touch.js
- I only need to pass the
middlewares
object into other modules to access its methods e.g. middlewares
can be passed into my routes
module and I can reference the touch middleware like middlewares.touch()
Very simply; I wish to require
all my middlewares and pass them into the routes module like:
...
var middlewares = require('./middlewares');
...
routes = require('./routes')(app, middlewares);
...
And use my middlewares in a route like:
/*jslint es5: true, indent: 2, node:true, nomen: true, maxlen: 80, vars: true*/
'use strict';
var routes = function (app, middlewares) {
var that = {};
app.get(
'/',
middlewares.touch,
function (req, res) {
res.send('Hello world!');
}
);
that.auth = require('./auth')(app);
return that;
};
module.exports = routes;
How should I go about that? Look at the top of this question to see how I'm trying (and failing) to do it now, thanks everyone for their help!
In addition, should modules return
something, e.g.; the routes
module doesn't really need to return anything, or does/should it?