提交 020e3738da3e4d9f3ef7e8bc864beeaedf02aa17
1 个父辈
6cb9a217
Updated express (to v4), added basic electrolyte functionality
正在显示
8 个修改的文件
包含
145 行增加
和
26 行删除
| 1 | 1 | |
| 2 | 2 | // # home |
| 3 | 3 | |
| 4 | -module.exports = home | |
| 4 | +exports = module.exports = home | |
| 5 | 5 | |
| 6 | -function home(req, res, next) { | |
| 7 | - res.render('home') | |
| 6 | +function home(app, lib, modeluser) { | |
| 7 | + return { | |
| 8 | + | |
| 9 | + render: function(req, res, next) { | |
| 10 | + res.render('home') | |
| 11 | + }, | |
| 12 | + | |
| 13 | + about: function(req, res, next) { | |
| 14 | + // save project data | |
| 15 | + var User = modeluser; | |
| 16 | + | |
| 17 | + console.log(User); | |
| 18 | + | |
| 19 | + /*var user = new User({ | |
| 20 | + email: 'user@example.com', | |
| 21 | + }); | |
| 22 | +*/ | |
| 23 | + var upsertData = User;//user.toObject(); | |
| 24 | + delete upsertData._id; | |
| 25 | + | |
| 26 | + User.update({email: 'user@example.com'}, upsertData, {upsert: true}, function( err, project ) { | |
| 27 | + | |
| 28 | + if(err){ | |
| 29 | + res.send(500, {message: 'Error saving project data'}); | |
| 30 | + return console.error('Error creating project data'); | |
| 31 | + } | |
| 32 | + | |
| 33 | + res.send(200, {message: 'success'}); | |
| 34 | + | |
| 35 | + }); | |
| 36 | + | |
| 37 | + res.send(200, 'Hello, this is Igloo.') | |
| 38 | + } | |
| 39 | + | |
| 40 | + } | |
| 8 | 41 | } |
| 42 | + | |
| 43 | +exports['@require'] = [ 'mongo', 'redis', 'models/user' ]; | |
| \ No newline at end of file | ... | ... |
| ... | ... | @@ -7,7 +7,16 @@ var dynamicHelpers = require('./dynamic-helpers') |
| 7 | 7 | var development = require('./development') |
| 8 | 8 | var production = require('./production') |
| 9 | 9 | var auth = require('./auth') |
| 10 | +var settings = require('./settings') | |
| 10 | 11 | var routes = require('./routes') |
| 12 | +var ioc = require('electrolyte') | |
| 13 | +var session = require('express-session') | |
| 14 | +var methodOverride = require('method-override') | |
| 15 | +var errorHandler = require('errorhandler') | |
| 16 | +var serveFavicon = require('serve-favicon') | |
| 17 | +var cookieParser = require('cookie-parser') | |
| 18 | +var bodyParser = require('body-parser') | |
| 19 | + | |
| 11 | 20 | |
| 12 | 21 | module.exports = function(lib, callback) { |
| 13 | 22 | |
| ... | ... | @@ -22,6 +31,12 @@ module.exports = function(lib, callback) { |
| 22 | 31 | // trust proxy |
| 23 | 32 | app.enable('trust proxy') |
| 24 | 33 | |
| 34 | + // set-up electrolyte | |
| 35 | + ioc.loader('controllers', ioc.node('app/controllers')); | |
| 36 | + ioc.loader('models', ioc.node('app/models')); | |
| 37 | + ioc.loader(ioc.node('config')); | |
| 38 | + ioc.loader(ioc.node('lib')); | |
| 39 | + | |
| 25 | 40 | // set the default views directory |
| 26 | 41 | app.set('views', lib.config.viewsDir) |
| 27 | 42 | |
| ... | ... | @@ -32,27 +47,31 @@ module.exports = function(lib, callback) { |
| 32 | 47 | app.locals.pretty = true |
| 33 | 48 | |
| 34 | 49 | // ignore GET /favicon.ico |
| 35 | - app.use(express.favicon(lib.config.favicon)) | |
| 50 | + app.use(serveFavicon(lib.config.favicon)) | |
| 36 | 51 | |
| 37 | 52 | // development only config |
| 38 | - app.configure('development', development(lib, app)) | |
| 53 | + switch(lib.config.env){ | |
| 54 | + case 'development': | |
| 55 | + development(lib, app); | |
| 56 | + break; | |
| 39 | 57 | |
| 40 | - // production only config | |
| 41 | - app.configure('production', production(lib, app)) | |
| 58 | + case 'production': | |
| 59 | + production(lib, app) | |
| 60 | + break; | |
| 61 | + } | |
| 42 | 62 | |
| 43 | 63 | // pass a secret to cookieParser() for signed cookies |
| 44 | - app.use(express.cookieParser(lib.config.cookieParser)) | |
| 64 | + app.use(cookieParser(lib.config.cookieParser)) | |
| 45 | 65 | |
| 46 | 66 | // parse request bodies |
| 47 | - app.use(express.json()) | |
| 48 | - app.use(express.urlencoded()) | |
| 67 | + app.use(bodyParser()) | |
| 49 | 68 | |
| 50 | 69 | // support _method (PUT in forms etc) |
| 51 | - app.use(express.methodOverride()) | |
| 70 | + app.use(methodOverride()) | |
| 52 | 71 | |
| 53 | 72 | // add req.session cookie support |
| 54 | - app.use(express.session(lib.config.session)) | |
| 55 | - | |
| 73 | + app.use(session(lib.config.session)) | |
| 74 | + | |
| 56 | 75 | // add flash message support |
| 57 | 76 | app.use(flash()) |
| 58 | 77 | |
| ... | ... | @@ -60,19 +79,18 @@ module.exports = function(lib, callback) { |
| 60 | 79 | app.use(dynamicHelpers) |
| 61 | 80 | |
| 62 | 81 | // add support for authentication |
| 63 | - app.configure(auth(lib, app)) | |
| 82 | + auth(lib, app)() | |
| 64 | 83 | |
| 65 | 84 | // load the routes |
| 66 | - app.configure(routes(lib, app)) | |
| 67 | - | |
| 68 | - // make the middleware order matter | |
| 69 | - app.use(app.router) | |
| 85 | + routes(lib, app)() | |
| 70 | 86 | |
| 71 | 87 | // static server |
| 72 | 88 | app.use(express.static(lib.config.publicDir, lib.config.staticServer)) |
| 73 | 89 | |
| 74 | 90 | // error handling |
| 75 | - app.use(express.errorHandler()) | |
| 91 | + //app.use(express.errorHandler()) | |
| 92 | + app.use(errorHandler()) | |
| 93 | + | |
| 76 | 94 | |
| 77 | 95 | callback(null, app) |
| 78 | 96 | ... | ... |
| 1 | 1 | |
| 2 | 2 | // # routes |
| 3 | 3 | |
| 4 | +var IoC = require('electrolyte') | |
| 5 | + | |
| 4 | 6 | var lib, app |
| 5 | 7 | |
| 8 | + | |
| 6 | 9 | module.exports = function(_lib, _app) { |
| 7 | 10 | lib = _lib |
| 8 | 11 | app = _app |
| ... | ... | @@ -10,9 +13,10 @@ module.exports = function(_lib, _app) { |
| 10 | 13 | } |
| 11 | 14 | |
| 12 | 15 | function routes() { |
| 16 | + var self = app; | |
| 13 | 17 | |
| 14 | - var controllers = require('../app/controllers')(lib) | |
| 15 | - | |
| 16 | - app.get('/', controllers.home) | |
| 18 | + var home = IoC.create('controllers/home') | |
| 17 | 19 | |
| 20 | + app.get('/', home.render) | |
| 21 | + app.get('/about', home.about) | |
| 18 | 22 | } | ... | ... |
config/settings.js
0 → 100644
| 1 | +/** | |
| 2 | + * Module dependencies. | |
| 3 | + */ | |
| 4 | +var config = require('./index'); | |
| 5 | + | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * Initialize settings. | |
| 9 | + * | |
| 10 | + * This component configures the application's settings. | |
| 11 | + */ | |
| 12 | +exports = module.exports = function(lib, app) { | |
| 13 | + var settings = new Settings(config); | |
| 14 | + return settings; | |
| 15 | +} | |
| 16 | + | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Component annotations. | |
| 20 | + */ | |
| 21 | +exports['@singleton'] = true; | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | +function Settings(initial) { | |
| 26 | + if(initial) | |
| 27 | + this._hash = initial; | |
| 28 | + else | |
| 29 | + this._hash = {}; | |
| 30 | +} | |
| 31 | + | |
| 32 | +Settings.prototype.get = function(key) { | |
| 33 | + return this._hash[key]; | |
| 34 | +} | |
| 35 | + | |
| 36 | +Settings.prototype.set = function(key, val) { | |
| 37 | + this._hash[key] = val; | |
| 38 | +} | |
| \ No newline at end of file | ... | ... |
| ... | ... | @@ -2,7 +2,8 @@ |
| 2 | 2 | // # redis |
| 3 | 3 | |
| 4 | 4 | var express = require('express') |
| 5 | -var RedisStore = require('connect-redis')(express) | |
| 5 | +var session = require('express-session') | |
| 6 | +var RedisStore = require('connect-redis')(session) | |
| 6 | 7 | |
| 7 | 8 | var config |
| 8 | 9 | |
| ... | ... | @@ -11,6 +12,11 @@ module.exports = function(_config) { |
| 11 | 12 | return redis |
| 12 | 13 | } |
| 13 | 14 | |
| 15 | +/** | |
| 16 | + * Component annotations. | |
| 17 | + */ | |
| 18 | +exports['@singleton'] = true; | |
| 19 | + | |
| 14 | 20 | function redis(callback) { |
| 15 | 21 | var connection = new RedisStore(config.redis) |
| 16 | 22 | connection.on('error', callback) | ... | ... |
| ... | ... | @@ -20,9 +20,9 @@ |
| 20 | 20 | "passport": "~0.2.0", |
| 21 | 21 | "express-rate": "0.0.1", |
| 22 | 22 | "jade": "~1.1.5", |
| 23 | - "express": "~3.4.8", | |
| 23 | + "express": "~4.2.0", | |
| 24 | 24 | "mongoose": "~3.8.7", |
| 25 | - "connect-redis": "~1.4.6", | |
| 25 | + "connect-redis": "~2.0.0", | |
| 26 | 26 | "underscore": "~1.6.0", |
| 27 | 27 | "async": "~0.2.10", |
| 28 | 28 | "winston": "~0.7.2", |
| ... | ... | @@ -30,6 +30,14 @@ |
| 30 | 30 | "less-middleware": "~0.1.15", |
| 31 | 31 | "connect-flash": "~0.1.1", |
| 32 | 32 | "winston-mongodb": "~0.4.3", |
| 33 | - "winston-request-logger": "~1.0.4" | |
| 33 | + "winston-request-logger": "~1.0.4", | |
| 34 | + "electrolyte": "*", | |
| 35 | + "express-session": "~1.1.0", | |
| 36 | + "method-override": "~1.0.1", | |
| 37 | + "static-favicon": "~2.0.0-alpha", | |
| 38 | + "cookie-parser": "~1.1.0", | |
| 39 | + "errorhandler": "~1.0.1", | |
| 40 | + "body-parser": "~1.2.0", | |
| 41 | + "serve-favicon": "^2.0.0" | |
| 34 | 42 | } |
| 35 | 43 | } | ... | ... |
请
注册
或
登录
后发表评论