express-scaffold
Advanced tools
Comparing version 0.0.5 to 0.0.7
@@ -1,69 +0,77 @@ | ||
var model = require('../model/index'); | ||
var matcher = function(id) { | ||
return (id && id.match(/^[0-9a-fA-F]{24}$/)); | ||
} | ||
var Ctrler = function(type) { | ||
this.type = (type && model[type]) ? type : null; | ||
var Ctrler = function(model) { | ||
this.model = model ? model : null; | ||
} | ||
// create model instance | ||
Ctrler.prototype.create = function(baby, cb) { | ||
if (this.type) { | ||
var baby = new model[this.type](baby); | ||
baby.save(function(err) { | ||
cb(err, baby); | ||
}); | ||
} | ||
Ctrler.prototype.checkId = matcher; | ||
Ctrler.prototype.create = function(baby, callback) { | ||
this.model.create(baby, callback); | ||
} | ||
// remove by ObjectID | ||
Ctrler.prototype.remove = function(id, cb) { | ||
if (this.type) { | ||
model[this.type].findByIdAndRemove(id, function(err) { | ||
cb(err, id); | ||
}); | ||
} | ||
Ctrler.prototype.findById = function(id, callback) { | ||
if (matcher(id)) this.model.findById(id).exec(callback); | ||
} | ||
// update by ObjectID | ||
Ctrler.prototype.update = function(id, baby, cb) { | ||
if (this.type) { | ||
model[this.type].findByIdAndUpdate(id, baby, function(err, result) { | ||
cb(err, result); | ||
}); | ||
} | ||
Ctrler.prototype.update = function(id, update, callback) { | ||
if (matcher(id)) this.model.findByIdAndUpdate(id, update, callback); | ||
} | ||
// read single data by ObjectID | ||
Ctrler.prototype.read = function(id, cb) { | ||
if (this.type) { | ||
model[this.type].findById(id).exec(function(err, body){ | ||
cb(err, body); | ||
}); | ||
} | ||
Ctrler.prototype.updates = function(params, update, callback) { | ||
if (matcher(id)) this.model.update(params, update, callback); | ||
} | ||
// list all collection | ||
Ctrler.prototype.list = function(cb) { | ||
if (this.type) { | ||
model[this.type].find({}).exec(function(err, body){ | ||
cb(err, body); | ||
}); | ||
} | ||
Ctrler.prototype.updateOne = function(id, update, callback) { | ||
if (matcher(id)) this.model.findOneAndUpdate(id, update, callback); | ||
} | ||
// advanced find | ||
Ctrler.prototype.find = function(params, cb) { | ||
if (this.type) { | ||
model[this.type].find(params).exec(function(err, body){ | ||
cb(err, body); | ||
}); | ||
} | ||
Ctrler.prototype.remove = function(id, callback) { | ||
if (matcher(id)) this.model.findByIdAndRemove(id, callback); | ||
} | ||
// advanced findOne | ||
Ctrler.prototype.findOne = function(params, cb) { | ||
if (this.type) { | ||
model[this.type].findOne(params).exec(function(err, body){ | ||
cb(err, body); | ||
}); | ||
} | ||
} | ||
Ctrler.prototype.removes = function(params, callback) { | ||
if (matcher(id)) this.model.remove(params, callback); | ||
} | ||
Ctrler.prototype.removeOne = function(id, callback) { | ||
if (matcher(id)) this.model.findOneAndRemove(id, callback); | ||
} | ||
Ctrler.prototype.populate = function(doc, params, callback) { | ||
this.model.populate(doc, params, callback); | ||
} | ||
Ctrler.prototype.count = function(params, callback) { | ||
var cb = (!callback && typeof(params) === 'function') ? params : callback, | ||
query = (params && typeof(params) === 'object') ? params : {}; | ||
this.model.count(query, cb); | ||
} | ||
Ctrler.prototype.list = function(params, callback) { | ||
var cb = (!callback && typeof(params) === 'function') ? params : callback, | ||
query = (params && typeof(params) === 'object') ? params : {}; | ||
this.model.find(query).exec(cb); | ||
} | ||
Ctrler.prototype.page = function(page, limit, params, callback) { | ||
var self = this, | ||
from = (page && page > 1) ? ((page - 1) * limit) : 0; | ||
self.model.count(params).exec(function(err, count) { | ||
if (!err) { | ||
self.model.find(params).skip(from).limit(limit).exec(function(err, results) { | ||
callback(err, results, { | ||
limit: limit, | ||
current: page ? page : 1, | ||
max: Math.round(count / limit) | ||
}); | ||
}); | ||
} else { | ||
callback(err); | ||
} | ||
}); | ||
} | ||
module.exports = Ctrler; |
@@ -1,3 +0,6 @@ | ||
var Ctrler = require('./index'); | ||
var Ctrler = require('./index'), | ||
model = require('../model/index'); | ||
module.exports = new Ctrler('user'); | ||
var User = new Ctrler(model.user); | ||
module.exports = User; |
{ | ||
"name": "express-scaffold", | ||
"version": "0.0.5", | ||
"version": "0.0.7", | ||
"description": "a simple MVC scaffold of Express project for production", | ||
@@ -5,0 +5,0 @@ "logo":"http://ww3.sinaimg.cn/large/61ff0de3jw1e91jmudlz8j201o01o0sj.jpg", |
@@ -1,4 +0,5 @@ | ||
// index page | ||
module.exports = function(req, res, next) { | ||
res.send('Hi, express-scaffold just works!'); | ||
}; | ||
var home = require('./home'); | ||
module.exports = function(app) { | ||
app.get('/', home); | ||
} |
@@ -1,4 +0,1 @@ | ||
// @brief : [project brief] | ||
// @author : [project author](project site) | ||
var express = require('express'), | ||
@@ -8,68 +5,48 @@ http = require('http'), | ||
MongoStore = require('connect-mongo')(express), | ||
pkg = require('./lib/pkg'), | ||
less = require('less-middleware'), | ||
json = require('./libs/json'), | ||
sys = require('./package.json'), | ||
errors = require('./lib/error'); | ||
errors = require('./middlewares/error'); | ||
var Server = function(params) { | ||
var Server = function(configs) { | ||
if (params) { | ||
var app = express(), | ||
router = require('./routes/index'); | ||
params = configs ? configs : {}, | ||
secret = params.database && params.database.name ? params.database.name : sys.name; | ||
var app = express(), | ||
self = this; | ||
if (params.database) json.save(path.join(__dirname, '/database.json'), params.database); | ||
pkg.set(path.join(__dirname, '/database.json'), params.database); | ||
// all environments | ||
app.set('env', params.env && typeof(params.env) == 'string' ? params.env : 'development'); | ||
app.set('views', path.join(__dirname, '/views')); | ||
app.set('view engine', 'jade'); | ||
app.use(express.favicon()); | ||
app.use(express.logger('dev')); | ||
app.use(express.bodyParser({keepExtensions: true,uploadDir: path.join(__dirname, '/public/uploads')})); | ||
app.use(express.methodOverride()); | ||
app.use(express.cookieParser(secret)); | ||
app.use(express.session({secret: secret,store: new MongoStore({db: secret,collection: 'sessions'})})); | ||
app.use(less({src: path.join(__dirname, 'public')})); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.use(app.router); | ||
// routers | ||
var index = require('./routes/index'), | ||
sign = require('./routes/sign'); | ||
// errors | ||
app.use(errors.logger); | ||
app.use(errors.xhr); | ||
app.use(errors.common); | ||
// all environments | ||
app.set('env', params.env ? params.env : 'development'); | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'jade'); | ||
app.use(express.favicon()); | ||
app.use(express.logger('dev')); | ||
app.use(express.bodyParser({ | ||
keepExtensions: true, | ||
uploadDir: path.join(__dirname, '/public/uploads') | ||
})); | ||
app.use(express.methodOverride()); | ||
app.use(express.cookieParser(params.database.name)); | ||
app.use(express.session({ | ||
secret: params.database.name, | ||
store: new MongoStore({ | ||
db: params.database.name, | ||
collection: 'sessions' | ||
}) | ||
})); | ||
app.use(function(req, res, next) { | ||
if (!res.locals._app) res.locals._app = self.app; | ||
next(); | ||
}); | ||
app.use(require('less-middleware')({ | ||
src: __dirname + '/public' | ||
})); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
app.use(app.router); | ||
// locals | ||
app.locals.sys = sys; | ||
app.locals.site = params; | ||
// locals | ||
app.locals.sys = sys; | ||
app.locals.site = params; | ||
// routes | ||
router(app); | ||
// errors | ||
app.use(errors.logger); | ||
app.use(errors.xhr); | ||
app.use(errors.common); | ||
// 404 | ||
app.get('*', errors.notfound) | ||
// home | ||
app.get('/', sign.passport, index); | ||
this.app = app; | ||
// 404 | ||
app.get('*', errors.notfound) | ||
self.app = app; | ||
self.params = params; | ||
} | ||
return this; | ||
} | ||
@@ -80,4 +57,3 @@ | ||
this.app.set('port', (port && !isNaN(parseInt(port, 10))) ? parseInt(port, 10) : defaultPort); | ||
this.app.locals.root = (this.app.get('env') === 'production') ? this.params.url : 'http://localhost:' + this.app.get('port'); | ||
console.log(this.app.locals); | ||
this.app.locals.url = (this.app.get('env') === 'production') ? this.params.url : 'http://localhost:' + this.app.get('port'); | ||
http.createServer(this.app).listen(this.app.get('port')); | ||
@@ -84,0 +60,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
308939
26
1390