Skip to content

Instantly share code, notes, and snippets.

@hogart
Created April 20, 2015 15:54
Show Gist options
  • Save hogart/10fe3ef5202bcabd18ba to your computer and use it in GitHub Desktop.
Save hogart/10fe3ef5202bcabd18ba to your computer and use it in GitHub Desktop.
function MainController(model) {
this.model = model;
}
MainController.prototype = {
findAll: function (req, res) {
res.send(200, this.model + '.find({})');
},
findById: function (req, res) {
res.send(200, this.model + '.findById(req.params.id)');
},
create: function (req, res) {
res.send(200, 'new ' + this.model + '(req.body)');
},
update: function (req, res) {
res.send(200, this.model + 'findByIdAndUpdate')
}
};
module.exports = MainController;
var MainController = require('./main');
var UserController = function () {
UserController.super_.apply(this, '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.authorize(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;
var express = require('express');
var app = express();
var UserController = require('./controllers/user');
var userController = new UserController();
app.get('/users', userController.findAll.bind(userController));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment