Skip to content

Commit

Permalink
actualizado ej. tema-5 a express4 + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
werelax committed Apr 29, 2014
1 parent 9383707 commit d1e9816
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 143 deletions.
80 changes: 80 additions & 0 deletions tema5/digg_clone/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Modules */

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var Q = require('q');
var auth = require('./simpleauth');
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var models = require('./models');

var app = express();

/* Config */

var client = Q.ninvoke(MongoClient,
'connect',
'mongodb://127.0.0.1:27017/diggclone');

models.setClient(client);

client.fail(function(e) {
console.log('ERROR conectando a Mongo: ', e);
});

function extend() {
var args = [].slice.call(arguments);
return args.reduce(function(acc, el) {
for (var k in el) { acc[k] = el[k]; }
return acc;
});
}

Q.longStackSupport = true;

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(cookieSession({secret: 'asdf'}));
app.use(express.static(path.join(__dirname, 'public')));

/* Auth */

auth.setStrategy({
serializeUser: function(user) {
},
deserializeUser: function(userToken, cb) {
},
checkCredentials: function(username, pass, cb) {
}
});

/* Routing */

app.use('/', require('./routes/users'));
app.use('/posts', require('./routes/posts'));
app.use('/comments', require('./routes/comments'));

/* Errors */

app.use(function(req, res, next) {
res.send(404);
});

app.use(function(err, req, res, next) {
res.send(500);
});

module.exports = app;
7 changes: 7 additions & 0 deletions tema5/digg_clone/bin/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
var app = require('../app');
var port = process.env.PORT || 3000;

var server = app.listen(port, function() {
console.log('Express server listening on port ' + port);
});
Empty file.
5 changes: 5 additions & 0 deletions tema5/digg_clone/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = exports = Object.create(require('./utils'));

exports.user = require('./user');
exports.post = require('./post');
exports.comment = require('./comment');
Empty file added tema5/digg_clone/models/post.js
Empty file.
9 changes: 9 additions & 0 deletions tema5/digg_clone/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var op = require('./utils').op;
var user = op.bind({}, "users");

// ejemplo
user.checkCredentials = function(email, password) {
return user("findOne", {email: email, password: password});
};

module.exports = user;
24 changes: 24 additions & 0 deletions tema5/digg_clone/models/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var Q = require("q");

var client = null;

exports.setClient = function(mongoClient) {
client = mongoClient;
};

exports.op = function op(colname) {
var args = [].slice.call(arguments, 1);
return client
.then(function(db) {
return Q.ninvoke.apply(Q, [db.collection(colname)].concat(args));
})
.fail(function(err) {
console.log("[MongoDB]", err);
throw err;
});
};

exports.makeOp = function(col) {
var args = [].slice.call(arguments, 1);
return function () { return col.apply({}, args); };
};
20 changes: 14 additions & 6 deletions tema5/digg_clone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
"name": "redradix-nodejs-digg",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/start"
},
"dependencies": {
"express": "*",
"jade": "*",
"mongodb": "*",
"nodemailer": "*",
"validator": "*",
"q": "*"
"express": "~4.0.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"jade": "~1.3.0",
"q": "~1.0.1",
"method-override": "~1.0.0",
"cookie-session": "~1.0.1",
"mongodb": "~1.4.2"
}
}
27 changes: 27 additions & 0 deletions tema5/digg_clone/routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var express = require('express');
var resources = require('./resources');
var auth = require('../simpleauth');

var commentsController = {
index: function(req, res) {
},
show: function(req, res) {
},
create: function(req, res) {
},
update: function(req, res) {
},
"delete": function(req, res) {
},
vote: function(req, res) {
},
param: function(req, res, next, postId) {
}
};

var router = express.Router();
router.use(auth.requiresToken);
resources(router, 'comments', commentsController);
router.post('/:commentsid/vote/:vote', commentsController.vote);

module.exports = router;
27 changes: 27 additions & 0 deletions tema5/digg_clone/routes/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var express = require('express');
var resources = require('./resources');
var auth = require('../simpleauth');

var postsController = {
index: function(req, res) {
},
show: function(req, res) {
},
create: function(req, res) {
},
update: function(req, res) {
},
"delete": function(req, res) {
},
vote: function(req, res) {
},
param: function(req, res, next, postId) {
}
};

var router = express.Router();
router.use(auth.requiresToken);
resources(router, 'posts', postsController);
router.post('/:postsid/vote/:vote', postsController.vote);

module.exports = router;
10 changes: 10 additions & 0 deletions tema5/digg_clone/routes/resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function (router, name, controller) {
if (controller.index) router.get("/", controller.index);
if (controller["new"]) router.get("/new", controller["new"]);
if (controller.create) router.post("/", controller.create);
if (controller.show) router.get("/:"+name+"id", controller.show);
if (controller.edit) router.get("/:"+name+"id/edit", controller.edit);
if (controller.update) router.put("/:"+name+"id", controller.update);
if (controller["delete"]) router["delete"]("/:"+name+"id", controller["delete"]);
if (controller.param) router.param(name + "id", controller.param);
};
19 changes: 19 additions & 0 deletions tema5/digg_clone/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var express = require('express');
var resources = require('./resources');
var auth = require('../simpleauth');

var usersController = {
create: function(req, res) {
},
me: function(req, res) {
},
login: function(req, res) {
}
};

var router = express.Router();
router.post('/session', auth.createSession(), usersController.login);
router.post('/users', usersController.create);
router.get('/me', auth.requiresToken, usersController.me);

module.exports = router;
137 changes: 0 additions & 137 deletions tema5/digg_clone/server.js

This file was deleted.

0 comments on commit d1e9816

Please sign in to comment.