Skip to content

Instantly share code, notes, and snippets.

@kopasetik
Last active August 29, 2015 14:23
Show Gist options
  • Save kopasetik/5cc8ea545b4179277886 to your computer and use it in GitHub Desktop.
Save kopasetik/5cc8ea545b4179277886 to your computer and use it in GitHub Desktop.
Promises with MongoDB
var Promise = require('bluebird');
var MongoClient = Promise.promisifyAll(require('mongodb').MongoClient);
var secret = require('./secret.js');
// url for connecting to the db
var url = secret.db_url;
module.exports = function activateMongo(){
return MongoClient.connectAsync(url);
};
var SlideShare = require('slideshare'),
segredo = require('./secret.js'),
express = require('express'),
bodyParser = require("body-parser"),
fs = require('fs'),
http = require('http'),
https = require('https'),
tag = process.argv[2] || 'angular';
var routes = require('./routes/route');
var ss = new SlideShare(segredo.key, segredo.secret);
function gatherSlides(){
ss.getSlideshowsByTag(tag, {limit: 10}, function(result){
console.log(result.Tag.Slideshow[0]);
});
}
var app = express();
var server = http.createServer(app);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.set('views', './views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/client'));
app.use('/', routes);
server.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0', function(){
var addr = server.address();
console.log('Server listening at', addr.address + ':' + addr.port);
});
var router = require('express').Router();
var db = require('../db_native_mongo');
var Promise = require('bluebird');
router.route('/presentation')
.get(function getIndex(req, res){
ss.getSlideshowsByTag(tag, {limit: 10}, function(result){
var embed = result.Tag.Slideshow[0].Embed;
res.render('index', {embed: embed});
res.end();
});
});
router.route('/quiz')
.get(function getQuiz(req, res){
res.render('quiz', {questions: questions});
res.end();
});
router.route('/ng-quiz')
.get(function getNgQuiz(req, res){
res.render('ng-quiz');
res.end();
});
router.route('/signup')
.get(function(req, res) {
res.render('signup', {form:{}});
res.end();
})
.post(function(req, res) {
// check input
// if good, save to db
var b = req.body;
var u = new User(b.user_name, b.pass_word, b.first_name, b.last_name);
db().then(u.save).then(console.log)
// redirect to success page
.then(function(){
res.render('thank-you')
})
// otherwise, send back to form with errors
.catch(function(e){
var err = e;
res.render('signup', {form: req.body, message: err});
});
});
function User(uname, password, firstName, lastName){
if (notInstanceOf(this, User)){
return new User(uname, password, firstName, lastName);
}
this.uname = uname;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
}
User.prototype.save = function saveUserAsync(db){
// Get users collection
var collection = Promise.promisifyAll(db.collection('users'));
return collection.insertAsync({
userName: this.uname,
password: this.password,
firstName: this.firstName,
lastName: this.lastName
});
};
function assertType(obj, type) {
var msg;
if (obj === null) {
msg = 'null';
} else if (obj === undefined) {
msg = 'undefined';
} else if (notInstanceOf(obj, type)) {
msg = obj.constructor.name;
}
if (msg) {
throw new Error(msg + ' not instance of ' + type.name).stack;
}
}
function notInstanceOf(obj, type) {
return !instanceOf(obj, type);
}
function instanceOf(obj, type) {
return (obj.constructor === type);
}
module.exports = router;
doctype html
html
head
title Sign Up
link(rel="stylesheet", href="/css/main.css")
body
form(action="/signup", method="post")
label(for="firstName") First Name
input#firstName(name="first_name", required, value="#{form.first_name || ''}")
br
label(for="lastName") Last Name
input#lastName(name="last_name", required)
br
label(for="username") Username
input#username(name="user_name", required)
br
label(for="password") Password
input#password(type="password", name="pass_word", required)
.button
button(type="submit") Send away!
.error #{message}
doctype html
html
head
title Thank You
link(rel="stylesheet", href="/css/main.css")
body Thanks for signing up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment