Reputation: 865
I'm starting with node.js and javascript server side. I think, maybe I got confused with the module.exports thing. And I'm pretty sure it's a noob issue.
I'm trying to do a simple task :
oAuth - controler [oAuth.js]
module.exports = function() {
return {
tryLogin :function(Username,Password){
}
};
}
oAuth - dispatcher [route.js]
module.exports = function(app){
app.post('/api/oAuth/login', function(req, res){
console.log("Trying to log with :");
console.log(req.body.Username);
console.log(req.body.Password);
var oAuthCtrl = require('./oAuth.js');
var result = oAuthCtrl.tryLogin(req.body.Username,req.body.Password);
res.send(result);
});
}
And the console result is :
TypeError: Object function (width) {
return {
tryLogin :function(Username,Password){
}
};
} has no method 'tryLogin'
What I want is an oAuth object inside my oAuthCtrl variable. This way I'll be able to call my tryLogin method.
The end point here is to build an oAuth module using passeport.js with some block and page views and methods like register, tryLogin, logout etc...
Any help would be greatly appreciated.
Upvotes: 0
Views: 369
Reputation: 135415
You don't need to wrap tryLogin
in an anonymous function when you export it.
Simply put, module.exports
is an object. You could almost think of it as a sort of "return value" from calling require
on a script.
You just want
// oauth.js
function tryLogin {
// ...
}
module.exports.tryLogin = tryLogin;
Here's an alternative based on your comment
// oauth.js
function MyClass() {
}
MyClass.tryLogin = function tryLogin() {
// ...
};
module.exports = MyClass;
In the last line, we're effectively replacing the default empty object provided by module.exports
with our "class".
Upvotes: 1