Created
June 19, 2011 21:02
-
-
Save AMSTKO/1034750 to your computer and use it in GitHub Desktop.
nodejs - MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* default | |
*/ | |
module.exports = function (req, res, met) { | |
this.mvc.langFile('default'); | |
var src = this.mvc.template('index', {'user':['pingouchoux']}); | |
this.cfg.cache_controller = false; | |
return src; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* fr | |
*/ | |
module.exports = { | |
'welcome': 'Bienvenue {user} ! :)' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%=lang('welcome', {'user':user})%><br /> | |
<%=lang('fucklol312', {'user':user})%> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* xy app | |
*/ | |
exports.app = require('./toyfw.js')({ | |
srv : function (app, express, cfg) { | |
app.use(app.router); | |
app.use(express.logger()); | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser()); | |
app.use(express.methodOverride()); | |
app.use(express.session({ secret: cfg.secret })); | |
}, | |
cfg : { | |
'path': '/home/app/jonathan.am', | |
'host': 'jonathan.am', | |
'secret': 'lolcat', | |
'controller': 'default', // default controlleur | |
'cache_client': 'public, max-age=3600', | |
'cache_controller': false, | |
'lang': 'fr' | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
app = express.createServer(); | |
app.use(express.vhost('*xy.com', require('/home/app/xy.com/app.js').app)); | |
app.listen(80, 'ip'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ToyFW | |
* release 1.0 | |
*/ | |
module.exports = (function () { | |
var fs = require('fs'), | |
path = require('path'), | |
mime = require('mime'), | |
express = require('express'), | |
mongoose = require('mongoose'), | |
_ = require('underscore'), | |
app = express.createServer(); | |
return function (sgl) { | |
// config | |
sgl.cfg = _.extend({ | |
'path': './', | |
'host': 'undefined', | |
'secret': '123456789', | |
'controller': 'default', // default controlleur | |
'view': '', | |
'cache_client': 'public, max-age=3600', | |
'cache_controller': true, | |
'lang': 'fr', | |
'ts': Math.round(+new Date/1000), | |
'tsm': +new Date | |
}, sgl.cfg); | |
// mvc | |
sgl.mvc = { | |
// view file | |
view: function (file) { | |
file = __dirname + '/view/' + file + '.html'; | |
return path.existsSync(file) ? fs.readFileSync(file, 'utf8') : '{view <em>' + file + '</em> not found}'; | |
}, | |
// routing management | |
routing: function (req, res, met) { | |
var controller = req.url.split('/')[1], | |
file = __dirname + '/controller/' + (controller === '' ? this.cfg.controller : controller) + '.js', | |
prev_cache_controller = this.cfg.cache_controller; | |
if(path.existsSync(file)) { | |
this.cfg.view = req.url.split('/')[2] ? req.url.split('/')[2] : 'index'; | |
var src = require(file).call(this, req, res, met); | |
} | |
else { | |
file = __dirname + '/controller/' + this.cfg.controller + '.js'; | |
this.cfg.view = req.url.split('/')[1]; | |
var src = require(file).call(this, req, res, met); | |
} | |
res.send(src===404 ? '{page <em>' + req.url + '</em> not found}' : src); | |
if(!this.cfg.cache_controller) { | |
delete require.cache[file]; | |
} | |
this.cfg.cache_controller = prev_cache_controller; | |
}, | |
// model | |
model: function () { | |
//... | |
}, | |
// template | |
template: function (view, obj) { | |
_.templateSettings = { | |
interpolate : /<%=([\s\S]+?)%>/g | |
}; | |
obj = _.extend({'lang':this.lang, 'langObj':this.langObj}, obj); | |
return _.template(this.view(view))(obj); | |
}, | |
//langObj | |
langObj: {}, | |
// lang | |
lang: function (tag, obj) { | |
_.templateSettings = { | |
interpolate : /\{(.+?)\}/g | |
}; | |
return this.langObj[tag] ? _.template(this.langObj[tag])(obj) : '{lang tag <em>' + tag + '</em> not found}'; | |
}, | |
// langload | |
langFile: function (file) { | |
if(!sgl.cfg.lang) { | |
return; | |
} | |
var file = __dirname + '/lang/' + sgl.cfg.lang + '/' + file + '.lang.js'; | |
this.langObj = _.extend((path.existsSync(file) ? require(file) : {}), this.langObj); | |
} | |
}; | |
// tools | |
sgl.tool = require('./tools.js'); | |
// server | |
sgl.srv(app, express, sgl.cfg); | |
// static (/public) | |
app.get('/public*', function(req, res){ | |
filename = sgl.cfg.path + req.url; | |
if(req.url !== '/public/' && path.existsSync(filename)) { | |
fs.readFile(filename, "binary", function(err, file) { | |
res.writeHead(200, {'Content-Type': mime.lookup(filename), 'Cache-Control': sgl.cfg.cache_client}); | |
res.write(file, "binary"); | |
res.end(); | |
}); | |
} | |
else { | |
res.send('{file <em>' + req.url + '</em> not found}'); | |
} | |
}); | |
// request | |
app.get('*', function(req, res) { | |
sgl.mvc.routing.call(sgl, req, res, 'get'); | |
}); | |
app.post('*', function(req, res) { | |
sgl.mvc.routing.call(sgl, req, res, 'post'); | |
}); | |
console.log(sgl.cfg.host + ' [load]'); | |
return app; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment