Skip to content

Instantly share code, notes, and snippets.

@RexMorgan
Created October 24, 2011 04:31
Show Gist options
  • Save RexMorgan/1308378 to your computer and use it in GitHub Desktop.
Save RexMorgan/1308378 to your computer and use it in GitHub Desktop.
/**
* Module dependencies.
*/
var express = require('express'),
mongoose = require('mongoose'),
config = require('./config').Config,
app_port = process.env.C9_PORT || 3001;
var app = module.exports = express.createServer();
var db = mongoose.connect('mongodb://' + config.db_user + ':' + config.db_pass + '@' + config.db_host + ':' + config.db_port + '/' + config.db_name);
/*
* models
*
* all the model imports and mongoose mappings.
*/
console.log(require('./models/user').User);
mongoose.model('User', require('./models/user').User);
mongoose.model('Gallery', require('./models/gallery').Gallery);
mongoose.model('Piece', require('./models/piece').Piece);
var User = mongoose.model('User');
var Gallery = mongoose.model('Gallery');
var Piece = mongoose.model('Piece');
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.render('index', {
title: 'Express'
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
› node app.js
{ paths:
{ email:
{ enumValues: [],
regExp: null,
path: 'email',
instance: 'String',
validators: [Object],
setters: [Object],
getters: [],
options: [Object],
_index: [Object],
isRequired: true },
_password:
{ enumValues: [],
regExp: null,
path: '_password',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
name:
{ enumValues: [],
regExp: null,
path: 'name',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
biography:
{ enumValues: [],
regExp: null,
path: 'biography',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
domain:
{ enumValues: [],
regExp: null,
path: 'domain',
instance: 'String',
validators: [Object],
setters: [Object],
getters: [],
options: [Object],
_index: [Object],
isRequired: true },
created_at:
{ path: 'created_at',
instance: undefined,
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null,
defaultValue: [Function: now] },
last_login:
{ path: 'last_login',
instance: undefined,
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null,
defaultValue: [Function: now] },
galleries:
{ path: 'galleries',
instance: undefined,
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null,
casterConstructor: [Object],
caster: [Object],
defaultValue: [Function],
schema: [Object] },
_id:
{ path: '_id',
instance: 'ObjectID',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null,
defaultValue: [Function] } },
virtuals:
{ id: { getters: [Object], setters: [], options: {} },
password: { getters: [Object], setters: [], options: {} } },
inherits: {},
callQueue: [],
_indexes: [],
methods: { setPassword: [Function] },
statics: {},
tree:
{ email:
{ type: [Function: String],
required: true,
lowercase: true,
index: [Object] },
_password: [Function: String],
name: [Function: String],
biography: [Function: String],
domain:
{ type: [Function: String],
required: true,
lowercase: true,
index: [Object] },
created_at: { type: [Function: Date], default: [Function: now] },
last_login: { type: [Function: Date], default: [Function: now] },
galleries: [ [Object] ],
_id: { type: [Function: ObjectId], auto: true },
id: { getters: [Object], setters: [], options: {} },
password: { getters: [Object], setters: [], options: {} } },
options: { 'use$SetOnSave': true, safe: true } }
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at Mongoose.model (/home/rex/Projects/foilr/src/main/node_modules/mongoose/lib/index.js:173:13)
at Object.<anonymous> (/home/rex/Projects/foilr/src/main/app.js:21:10)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.<anonymous> (module.js:430:10)
at EventEmitter._tickCallback (node.js:126:26)
var mongoose = require('mongoose'),
bcrypt = require('bcrypt'),
Schema = mongoose.Schema,
Gallery = require('./gallery').Gallery;
var User = new Schema({
email: {
type: String,
required: true,
lowercase: true,
index: {
unique: true
}
},
_password : String,
name : String,
biography : String,
domain : {
type: String,
required: true,
lowercase: true,
index: {
unique: true
}
},
created_at : {type: Date, 'default': Date.now},
last_login : {type: Date, 'default': Date.now},
galleries : [Gallery]
});
User.path('email').validate(function (value, fn) {
mongoose.model('User').count({'email': value}, function (err, result) {
if(err) {
fn(false);
} else {
fn(result === 0);
}
});
}, 'This email is already in use.');
User.path('domain').validate(function (value, fn) {
mongoose.model('User').count({'domain': value}, function (err, result) {
if(err) {
fn(false);
} else {
fn(result === 0);
}
});
}, 'This domain is already in use.');
User.virtual('password').get(function() {
return this._password;
});
User.methods.setPassword = function (password, done) {
var _this = this;
bcrypt.gen_salt(10, function(err, salt) {
bcrypt.encrypt(password, salt, function(err, encrypted) {
_this._password = encrypted;
_this._salt = salt;
done();
});
});
};
exports.User = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment