-
Notifications
You must be signed in to change notification settings - Fork 11
philosophy for a framework
A shared object is an object that lives during a serving of a request.
It basically contains request , response, parsed url, query string. Also it can any variable you like to share between functions during a serving of a request.
Shared object sugar enables bricks like programing
- a shared query string
- a shared object
Basic structure:
- A function can decide what to do by looking on its variable inside a shared query string or suppose an item id is exists in a shared object then it loads something.
- A function that pre fills some data in a shared object
- A function that gets additional data by information that is pre stored inside a shared object by a previously called function.
- A function that serves the result to the user.
you do not need to be so simple or so modular also it can be asynchronous.
a very simplistic example:
var shared={ req , res, parsedurl, qs }
function init()
{
shared.mydata={};
}
function get_foo() // prefills some data it does it by a query string
{
if(shared.qs.numbar)
{
shared.mydata.foo=[];
for(i=0;i<shared.qs.numbar;i++)
shared.mydata.foo.push(i*1);
}
}
function get_bar() // prefills some data it does it by a query string
{
if(shared.qs.numbar)
{
shared.mydata.bar=[];
for(i=0;i<shared.qs.numbar;i++)
shared.mydata.bar.push(i*1);
}
}
function settemplate()
{
shared.template=function(name){return "hello "+name;};
}
function render()
{
shared.content=shared.template(Object.keys(shared.mydata).join('.'));
}
function send()
{
if(shared.headers)
shared.res.writeHead(200, shared.headers);
else
shared.res.writeHead(200, {'Content-Type': 'text/plain'});
shared.res.end(shared.content);
}
init()
get_foo()
get_bar()
settemplate()
render()
send()
making it async:
function init()
{
var shared=this.shared;
shared.mydata={};
this.next();
}
function get_foo() // prefills some data it does it by a query string
{
var shared=this.shared;
if(shared.qs.numbar)
{
shared.mydata.foo=[];
for(i=0;i<shared.qs.numbar;i++)
shared.mydata.foo.push(i*1);
}
this.next();
}
function get_bar() // prefills some data it does it by a query string
{
var shared=this.shared;
if(shared.qs.numbar)
{
shared.mydata.bar=[];
for(i=0;i<shared.qs.numbar;i++)
shared.mydata.bar.push(i*1);
}
this.next();
}
function settemplate()
{
var shared=this.shared;
shared.template=function(name){return "hello "+name;};
}
function render()
{
var shared=this.shared;
shared.content=shared.template(Object.keys(shared.mydata).join('.'));
this.next();
}
function send()
{
var shared=this.shared,
res=shared.res;
if(shared.headers) // custom headers
shared.res.writeHead(200, shared.headers);
else
shared.res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(shared.content);
}
this.next();
}
var shared={ req , res, parsedurl, qs }
var inflow=require('inflow'); //some async library I prefer mine because it includes a shared object support
// inflow.flow is like async.serial function but with a shared object. In the called function it is accessible as this.shared.
inflow.flow(shared,[init,get_foo,get_bar,settemplate,render,send]);
using include module with injection of variables (app).
this.main=function(app)
{
var module_scope=typeof global!=="undefined"?global:typeof window!=="undefined"?window:(function(){return this;})();
if(app) {for(n in app) {module_scope[n]=app[n];}} // maybe to make variables local
// code here
}
using include module with injection of variables (app). maybe to make variables local using eval.
this.main=function(app)
{
if(app) {
var vars="";
for(n in app) {
if(vars!=="") vars+=",";
vars+=n+"=app['"+n+"']";
}
if(vars!=="") {
vars="var "+vars;
eval(vars);
}
} // maybe to make variables local
// code here
}
calling the included module:
var controller = require('controller.js').main(app);
controller.somefunction();
a controller with some function:
this.main=function(app)
{
// code here
var controller={};
controller.myfunction=function()
{
}
return controller;
}
making of it a pluggable controller:
this.main=function(app)
{
// code here
var controller={};
controller.init=function()
{
var shared=this.shared;
shared.mydata={};
this.next();
}
controller.get_foo=function() // prefills some data it does it by a query string
{
var shared=this.shared;
if(shared.qs.numbar)
{
shared.mydata.foo=[];
for(i=0;i<shared.qs.numbar;i++)
shared.mydata.foo.push(i*1);
}
this.next();
}
controller.get_bar=function() // prefills some data it does it by a query string
{
var shared=this.shared;
if(shared.qs.numbar)
{
shared.mydata.bar=[];
for(i=0;i<shared.qs.numbar;i++)
shared.mydata.bar.push(i*1);
}
this.next();
}
controller.settemplate=function()
{
var shared=this.shared;
shared.template=function(name){return "hello "+name;};
}
controller.render=function()
{
var shared=this.shared;
shared.content=shared.template(Object.keys(shared.mydata).join('.'));
this.next();
}
controller.send=function()
{
var shared=this.shared,
res=shared.res;
if(shared.headers) // custom headers
res.writeHeaders(shared.headers);
else
{
res.writeHeaders( {'Content-Type':'text/html'} );
res.end(shared.content);
}
this.next();
}
controller.main=function(shared,callback)
{
var inflow=require('inflow');
// inflow.flow is like async.serial function but with a shared object. In the called function it is accessible as this.shared.
inflow.flow(shared,[init,get_foo,get_bar,settemplate,render,send,callback]);
}
return controller;
}
the code that will be used will look like:
var controller = require('controller.js').main(app);
var shared={ req: req, res:res, parsedurl:url, qs:url.querystring };
controller.main(shared,function(){
console.log('thank you');
});
now as an http server
var url = require('url');
var http = require('http');
var app={};
app.fs=require('fs'); // shared loaded modules
http.createServer(function (req, res) {
var controller = require('controller.js').main(app);
var shared={ req: req, res:res, parsedurl:url, qs:url.querystring };
controller.main(shared,function(){
console.log('thank you');
});
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');
This document is in process. Everyone is encouraged to participate.
Inter-controller communication
Additions and Comments are welcome in this page:
Comments:
also you can post an issue at https://github.com/shimondoodkin/nodejs-mongodb-app/issues
Philosophy for a framework, with practical examples: