From d1e9816b791fd1ae80f8b7e4a67efcfeb8cea526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?El=C3=ADas=20Alonso?= Date: Tue, 29 Apr 2014 18:39:51 +0200 Subject: [PATCH] actualizado ej. tema-5 a express4 + refactor --- tema5/digg_clone/app.js | 80 ++++++++++++++++ tema5/digg_clone/bin/start | 7 ++ tema5/digg_clone/models/comment.js | 0 tema5/digg_clone/models/index.js | 5 + tema5/digg_clone/models/post.js | 0 tema5/digg_clone/models/user.js | 9 ++ tema5/digg_clone/models/utils.js | 24 +++++ tema5/digg_clone/package.json | 20 ++-- tema5/digg_clone/routes/comments.js | 27 ++++++ tema5/digg_clone/routes/posts.js | 27 ++++++ tema5/digg_clone/routes/resources.js | 10 ++ tema5/digg_clone/routes/users.js | 19 ++++ tema5/digg_clone/server.js | 137 --------------------------- 13 files changed, 222 insertions(+), 143 deletions(-) create mode 100644 tema5/digg_clone/app.js create mode 100755 tema5/digg_clone/bin/start create mode 100644 tema5/digg_clone/models/comment.js create mode 100644 tema5/digg_clone/models/index.js create mode 100644 tema5/digg_clone/models/post.js create mode 100644 tema5/digg_clone/models/user.js create mode 100644 tema5/digg_clone/models/utils.js create mode 100644 tema5/digg_clone/routes/comments.js create mode 100644 tema5/digg_clone/routes/posts.js create mode 100644 tema5/digg_clone/routes/resources.js create mode 100644 tema5/digg_clone/routes/users.js delete mode 100644 tema5/digg_clone/server.js diff --git a/tema5/digg_clone/app.js b/tema5/digg_clone/app.js new file mode 100644 index 0000000..cfa566e --- /dev/null +++ b/tema5/digg_clone/app.js @@ -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; diff --git a/tema5/digg_clone/bin/start b/tema5/digg_clone/bin/start new file mode 100755 index 0000000..7d38a2e --- /dev/null +++ b/tema5/digg_clone/bin/start @@ -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); +}); diff --git a/tema5/digg_clone/models/comment.js b/tema5/digg_clone/models/comment.js new file mode 100644 index 0000000..e69de29 diff --git a/tema5/digg_clone/models/index.js b/tema5/digg_clone/models/index.js new file mode 100644 index 0000000..e9cb7f7 --- /dev/null +++ b/tema5/digg_clone/models/index.js @@ -0,0 +1,5 @@ +module.exports = exports = Object.create(require('./utils')); + +exports.user = require('./user'); +exports.post = require('./post'); +exports.comment = require('./comment'); diff --git a/tema5/digg_clone/models/post.js b/tema5/digg_clone/models/post.js new file mode 100644 index 0000000..e69de29 diff --git a/tema5/digg_clone/models/user.js b/tema5/digg_clone/models/user.js new file mode 100644 index 0000000..4363600 --- /dev/null +++ b/tema5/digg_clone/models/user.js @@ -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; diff --git a/tema5/digg_clone/models/utils.js b/tema5/digg_clone/models/utils.js new file mode 100644 index 0000000..d61ce61 --- /dev/null +++ b/tema5/digg_clone/models/utils.js @@ -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); }; +}; diff --git a/tema5/digg_clone/package.json b/tema5/digg_clone/package.json index cb35d1d..3f08d5d 100644 --- a/tema5/digg_clone/package.json +++ b/tema5/digg_clone/package.json @@ -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" } } diff --git a/tema5/digg_clone/routes/comments.js b/tema5/digg_clone/routes/comments.js new file mode 100644 index 0000000..f7aff9c --- /dev/null +++ b/tema5/digg_clone/routes/comments.js @@ -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; diff --git a/tema5/digg_clone/routes/posts.js b/tema5/digg_clone/routes/posts.js new file mode 100644 index 0000000..59faa35 --- /dev/null +++ b/tema5/digg_clone/routes/posts.js @@ -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; diff --git a/tema5/digg_clone/routes/resources.js b/tema5/digg_clone/routes/resources.js new file mode 100644 index 0000000..283af56 --- /dev/null +++ b/tema5/digg_clone/routes/resources.js @@ -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); +}; diff --git a/tema5/digg_clone/routes/users.js b/tema5/digg_clone/routes/users.js new file mode 100644 index 0000000..cbb940f --- /dev/null +++ b/tema5/digg_clone/routes/users.js @@ -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; diff --git a/tema5/digg_clone/server.js b/tema5/digg_clone/server.js deleted file mode 100644 index 172d1f2..0000000 --- a/tema5/digg_clone/server.js +++ /dev/null @@ -1,137 +0,0 @@ -/* Dependencies */ - -var express = require("express") - , Q = require("q") - , check = require("validator").check - , auth = require("./simpleauth") - , mailer = require("nodemailer") - , MongoClient = require("mongodb").MongoClient - , ObjectID = require("mongodb").ObjectID; - -var client = Q.ninvoke(MongoClient, - "connect", - "mongodb://127.0.0.1:27017/diggclone"); - -client.fail(function(e) { - console.log("ERROR conectando a Mongo: ", e); -}); - -/* Solo para los que tengáis tiempo y sendmail */ - -var mailerTransport = mailer.createTransport("Sendmail", "/usr/sbin/sendmail"); - -/* Para enviar un correo: mailerTransport.sendMail({to: "", from: "", subject: "", text: "", html:""}) */ - -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; - -var app = express(); - -app.configure(function(){ - app.set('port', process.env.PORT || 3000); - app.use(express.favicon()); - app.use(express.logger('short')); - app.use(express.bodyParser()); - app.use(express.methodOverride()); - - app.use(express.cookieParser('your secret here')); - app.use(express.session()); - - app.use(app.router); - app.use(express.static(__dirname + '/public')); -}); - -/* Auth */ - -auth.setStrategy({ - serializeUser: function(user) { - }, - deserializeUser: function(userToken, cb) { - }, - checkCredentials: function(username, pass, cb) { - } -}); - -/* Controllers */ - -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 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, commentId) { - } -}; - -var usersController = { - create: function(req, res) { - }, - me: function(req, res) { - }, - login: function(req, res) { - } -}; - -/* Routing */ - -function resources(app, name, controller) { - if (controller.index) app.get("/"+name, controller.index); - if (controller["new"]) app.get("/"+name+"/new", controller["new"]); - if (controller.create) app.post("/"+name, controller.create); - if (controller.show) app.get("/"+name+"/:"+name+"id", controller.show); - if (controller.edit) app.get("/"+name+"/:"+name+"id/edit", controller.edit); - if (controller.update) app.put("/"+name+"/:"+name+"id", controller.update); - if (controller["delete"]) app["delete"]("/"+name+"/:"+name+"id", controller["delete"]); - if (controller.param) app.param(name + "id", controller.param); -} - -app.post("/session", auth.createSession(), usersController.login); -app.post("/users", usersController.create); -app.get("/me", auth.requiresToken, usersController.me); - -auth.withToken(app, function(app) { - resources(app, "posts", postsController); - app.post("/posts/:postsid/vote/:vote", postsController.vote); - resources(app, "comments", commentsController); - app.post("/comments/:commentsid/vote/:vote", commentsController.vote); -}); - -app.use(function(req, res, next) { - res.send(404); -}); - -app.use(function(err, req, res, next) { - res.send(500); -});