Genie is framework for Express3.x based to Klass.
$ npm install genie-express -g
$ genie create my-app
$ cd my-app $$ npm install genie-express
$ node server.js
$ genie controller [name]
$ genie model [name]
$ genie service [name]
$ genie factory [name]
module.exports = function (app, config) {
var ApplicationController = app.getController("Application", true);
var IndexController = ApplicationController.extend(function(){
this.routes = {
"index" : "/"
};
this.addBeforeAction("action", function(res, req, next){
var CheckFactory = app.getFactory("Check");
CheckFactory.start(res, req, next);
});
this.addBeforeAction("get", function(res, req, next){
var CheckFactory = app.getFactory("Check");
CheckFactory.start(res, req, next);
});
this.addBeforeAction("get", function(res, req, next){
var CheckService = app.getService("Check");
CheckService.start(res, req, next);
});
}).methods({
getIndex : function(req, res){
res.render("index", {
title : "Hello, Genie"
});
},
});
return IndexController;
}
'use strict';
module.exports = function(app){
var ApplicationModel = app.getModel('Application', true);
var CheckModel = ApplicationModel.extend(function(){
this.app = app;
this.modelName = "Check";
this.rooms = ['Apple', 'Orange', 'Grape'];
}).methods({
getRoom : function(id){
console.log(this.rooms);
return this.rooms[id];
}
});
return CheckModel;
};
'use strict';
module.exports = function(app){
var ApplicationService = app.getService('Application', true);
var CheckService = ApplicationService.extend(function(app){
this.app = app;
this.serviceName = "Check";
this.config = this.Config();
this.num = this.config.num;
}).methods({
start : function(req, res, next){
this.supr();
console.log("service: "+ this.num++);
next();
}
});
return CheckService;
};
'use strict';
module.exports = function(app){
var ApplicationFactory = app.getFactory('Application', true);
var CheckFactory = ApplicationFactory.extend(function(){
this.factoryName = "Check";
this.num = 0;
}).methods({
start : function(req, res, next){
this.supr();
console.log("factory: "+ this.num++);
next();
}
});
return CheckFactory;
};
Uses Twig to render Template
<div id="title">{% block content %}{% endblock %}</div>
{% extends "layout.twig" %}
{% block content %}
{{ title }}
{% endblock %}
Routings are extract from method of controller.
Extract methods are GET, POST, PUT and DELETE.
getIndex : function(req, res){
res.render("index", {
title : "Get index"
});
}
postCreate : function(req, res){
res.render("index", {
title : "Post create"
});
}
Params are extract from property of controller.
// controllers/test/TestController.js
module.exports = function (app, config) {
var ApplicationController = app.getController("Application", true);
var TestController = ApplicationController.extend(function(){
this.params = {
"member" : [":id"]
};
}).methods({
getMember : function(req, res){
res.render("index", {
title : "Genie is "+req.params.id
});
},
});
return TestController;
}
In the case of a different method?
this.params = {
"member" : {
get : [":id"],
post: [":test"]
}
};
Routes change extract from property of controller.
// controllers/test/TestController.js
module.exports = function (app, config) {
var ApplicationController = app.getController("Application", true);
var TestController = ApplicationController.extend(function(){
this.routes = {
"index": "/test/"
};
}).methods({
getIndex : function(req, res){
res.render("index", {
title : "Genie is test"
});
},
});
return TestController;
}
Service will create an instance for each call.
module.exports = function (app, config) {
var ApplicationController = app.getController("Application", true);
var TestController = ApplicationController.extend(function(){
this.TestService = this.app.getService("Test");
})
}
Factory will call the same instance.
module.exports = function (app, config) {
var ApplicationController = app.getController("Application", true);
var TestController = ApplicationController.extend(function(){
this.TestFactory = this.app.getFactory("Test");
})
}