forked from redradix-school/curso-node-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
actualizado ej. tema-5 a express4 + refactor
- Loading branch information
Showing
13 changed files
with
222 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.