Роутер
var UserController = require('../controllers/user');
var userController = new UserController();
//Вызовы унаследуемых методов
app.get('/users',userController.findAll.bind(userController));
app.get('/users/:id',userController.findById.bind(userController));
//наверняка неправильный вызов методов прототипа
app.post('/login',userController.checkAuth,userController.login);
//Дочерний
var util = require('util');
var User = require('../models/user').User;
ObjectID = require('mongodb').ObjectID;
var MainController = require('./index');
function UserController () {
UserController.super_.apply(this,arguments);
this.model = User;
};
UserController.prototype = {
checkAuth : function(req, res, next){
if (req.session.user) {
res.json({"login": req.session.user});
}
else{
next();
}
},
login: function(req, res, next){
var mail = req.body.mail;
var password = req.body.password;
User.autorize(mail,password,function(err, result, user){
if (result.login == 'yes'){
req.session.user = user._id;
res.json(result);
}
else{
res.json({"login":"no"});
}
}
)
}
};
util.inherits(UserController, MainController);
module.exports = UserController;
//Главный
function MainController(model) {
this.model = model;
}
MainController.prototype = {
findAll: function(req, res) {
this.model.find({}, function (err, results) {
if (err) res.json({"findAll":err});
res.json(results);
});
},
findById : function(req,res) {
var id = new ObjectID(req.params.id)
this.model.findById(id, function (err, results) {
if (err) res.json({"findById":err});
res.json(results);
})
},
create: function(req, res){
var saver = new this.model(req.body);
saver.save(function(err,results, affected){
if (err) throw err;
res.json({'create':'yes'});
});
},
update: function(req, res){
}
};
module.exports = MainController;