public
Last active

  • Download Gist
.gitignore
1
node_modules/*
db.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
var mongoose = require('mongoose');
var async = require('async');
 
mongoose.connect("mongodb://localhost/so_quotes");
 
// ===================================================
// Quote schema
 
var QuoteSchema = new mongoose.Schema({
quote: String
});
 
// Finds a random quote based on how many there are in the collection
 
QuoteSchema.statics.random = function(cb) {
this.count(function(err, count) {
if (err) return cb(err);
var rand = Math.floor(Math.random() * count);
this.findOne().skip(rand).exec(cb);
}.bind(this));
};
 
// Finds the 'next quote'
 
QuoteSchema.methods.next = function(cb) {
var model = this.model("Quote");
model.findOne().where('_id').gt(this._id).exec(function(err, quote) {
if (err) throw err;
 
if (quote) {
cb(null, quote);
} else {
// If quote is null, we've wrapped around.
model.findOne(cb);
}
});
};
 
var Quote = mongoose.model("Quote", QuoteSchema);
 
// ===================================================
// User schema
 
var UserSchema = new mongoose.Schema({
user: String,
lastQuote: { type: mongoose.Schema.Types.ObjectId, ref: 'Quote' }
});
 
var User = mongoose.model("User", UserSchema);
 
// ===================================================
// Clear out old data and re-initialize each time the program runs
 
function reset(callback) {
async.parallel([
Quote.remove.bind(Quote),
User.remove.bind(User)
], function(err) {
if (err) throw err;
callback();
});
}
 
// ===================================================
// Seed the database with initial data.
 
function seed(callback) {
quotes = [
"Chuck Norris counted to infinity - twice.",
"When the Boogeyman goes to sleep every night he checks his closet for Chuck Norris.",
"Chuck Norris has already been to Mars; that's why there are no signs of life there.",
"The chief export of Chuck Norris is pain.",
"There is no chin behind Chuck Norris' beard. There is only another fist.",
"When Chuck Norris had surgery, the anesthesia was applied to the doctors.",
"Chuck Norris is what Willis was talking about.",
"If you have five dollars and Chuck Norris has five dollars, Chuck Norris has more money than you.",
"Ironically, Chuck Norris' idden talent is invisibility."
];
 
async.parallel([
function(cb) {
async.forEachSeries(quotes, function(quote, eachCallback) {
var q = new Quote({quote: quote});
q.save(eachCallback);
}, cb);
},
function(cb) {
var user = new User({user: "JohnDoe"});
user.save(cb);
}
], function(err, results) {
if (err) throw err;
callback();
})
}
 
module.exports = {
Quote: Quote,
User: User,
reset: reset,
seed: seed
};
index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
var http = require('http');
var async = require('async');
var express = require('express');
var db = require('./db');
 
var User = db.User;
var Quote = db.Quote;
 
function startServer(callback) {
var app = express();
app.use(express.favicon());
 
// For each request we're the same user
app.all('*', function(req, res, next) {
User.findOne().populate('lastQuote').exec(function(err, user) {
if (err) return next(err);
req.user = user;
next();
});
});
 
app.get('/', function(req, res, next) {
if (req.user.lastQuote) {
req.user.lastQuote.next(function(err, quote) {
res.send(quote.quote);
req.user.lastQuote = quote._id;
req.user.save();
});
} else {
Quote.random(function(err, quote) {
res.send(quote.quote);
req.user.lastQuote = quote._id;
req.user.save();
});
}
});
 
http.createServer(app).listen(3000, callback);
}
 
async.series([db.reset, db.seed, startServer], function(err) {
if (err) throw err;
console.log("Server started.");
});
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
{
"name": "so_random_quote",
"version": "0.0.0",
"description": "ERROR: No README.md file found!",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "",
"license": "BSD",
"dependencies": {
"async": "~0.1.22",
"mongoose": "~3.0.3",
"express": "~3.0.0rc3"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.