Skip to content

Commit

Permalink
Début des développements des Forms
Browse files Browse the repository at this point in the history
  + Refactoring du debugger, du router
  + Router : Prise en compte des query string (?param=value)
  • Loading branch information
arno06 committed May 18, 2019
1 parent c298a25 commit 1ecceeb
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
25 changes: 17 additions & 8 deletions src/nws/middlewares/debugger.js
Expand Up @@ -5,7 +5,6 @@ let debug = true;
let content = {};
let ready = false;
let startTime;
let lastResponse;

let csl = console;

Expand Down Expand Up @@ -39,11 +38,18 @@ function formatMemory(pValue){
}

module.exports = {
errorHandler:function(pError){
lastResponse.setHeader("Content-Type", "text/html;charset=UTF-8");
lastResponse.writeHead(500);
lastResponse.write("<html><body><h1>Internal server error</h1></body></html>")
lastResponse.end();
errorHandler:function(pResponse, pError){
pResponse.setHeader("Content-Type", "text/html;charset=UTF-8");
pResponse.writeHead(500);
if(!ready){
pResponse.write("<html><body><h1>Internal server error</h1></body></html>");
}else{
let s = pError.stack.split("\n");
s.shift();
let str = s.reduce((pResult, pEntry)=>pResult+"<li>"+pEntry+"</li>", "<ul>")+"</ul>";
pResponse.write("<html><body><h1>"+pError.message+"</h1><div>"+str+"</div></body></html>");
}
pResponse.end();
},
middleware:function(pRequest, pResponse){
if(!debug){
Expand All @@ -55,7 +61,6 @@ module.exports = {
console:[],
memory:process.memoryUsage()
};
lastResponse = pResponse;
pResponse.realEnd = pResponse.end;
pResponse.end = function(){
let type = this.getHeader("Content-Type")?this.getHeader("Content-Type").toLowerCase():"";
Expand All @@ -72,11 +77,15 @@ module.exports = {
content.memory[k] = formatMemory(used[k] - content.memory[k]);
}
let id = "'NWS '+String.fromCodePoint(0x23F1)+' "+timeToGenerate+"s '+String.fromCodePoint(0x1F3C1)+' "+content.memory.heapUsed+"'";
let output = "console.group("+id+");";
let output = "console.groupCollapsed("+id+");";
output = content.console.reduce(function(pOutput, pEntry){
return pOutput+"console."+pEntry[0]+".apply(null,"+pEntry[1]+");";
}, output);
output += "console.groupEnd("+id+");";
id = "'NWS query params :'";
output += "console.groupCollapsed("+id+");";
output += "console.table.apply(null, ["+JSON.stringify(pRequest.query_params)+"]);";
output += "console.groupEnd("+id+");";
this.write("<script>"+output+"</script>", 'utf8');
this.realEnd();
};
Expand Down
10 changes: 6 additions & 4 deletions src/nws/middlewares/router.js
@@ -1,3 +1,4 @@
const querystring = require('querystring');
let routes = require("../../shared/routes");

module.exports = {
Expand All @@ -20,10 +21,11 @@ module.exports = {
url = url.replace('{$'+n+'}', "("+params[n]+")");
p.push(n);
}
let [request_url, query_string] = pRequest.url.split("?");
let re = new RegExp('^'+url+'$');
let matches = pRequest.url.match(re);
let matches = request_url.match(re);
let query_params = !query_string?{}:querystring.parse(query_string);
if(matches !== null){
let query_params = {};
p.forEach(function(pItem, pIndex){
query_params[pItem] = matches[pIndex+1];
});
Expand All @@ -35,11 +37,11 @@ module.exports = {
}else{
return false;
}
if(query_params.hasOwnProperty('module')){
if(query_params.module && r.indexOf('{$module}')!==-1){
r = r.replace('{$module}', query_params.module);
delete query_params.module;
}
if(query_params.hasOwnProperty('method')){
if(query_params.method && r.indexOf('{$method}')!==-1){
r = r.replace('{$method}', query_params.method);
delete query_params.method;
}
Expand Down
6 changes: 4 additions & 2 deletions src/nws/nws.js
Expand Up @@ -16,16 +16,18 @@ class NWS{
}

run(pPort = null){
this.middleware(router);
if(this.running){
return;
}
if(pPort !== null){
this.port = pPort;
}

console.log('\x1b[36m', 'NWS', '\x1b[0m','running at http://localhost:'+this.port+'/' ,'\x1b[0m');

var ref = this;
this.running = true;
this.middleware(router);
http.createServer(function(pRequest, pResponse){
try{
for(let i = 0, max = ref.stack.length; i<max; i++){
Expand All @@ -42,7 +44,7 @@ class NWS{
}
catch(e){
console.error(e);
errorHandler(e);
errorHandler(pResponse, e);
}
}).listen(this.port);
}
Expand Down
2 changes: 1 addition & 1 deletion src/nws/tools/template/Server.js
Expand Up @@ -53,7 +53,7 @@ class Template extends BaseTemplate
}
pResponse.setHeader("Content-Type", "text/html;charset=UTF-8");
pResponse.writeHead(200);
pResponse.write(this.evaluate(), 'utf8');
pResponse.write(result, 'utf8');
pResponse.end();
}
}
Expand Down
39 changes: 12 additions & 27 deletions src/routes/index.js
@@ -1,40 +1,25 @@
let Template = require("../nws/tools/template/Server");
let Form = require("../nws/tools/form/Server");

module.exports = {
index:function(pRequest, pResponse){

/**
let p1 = new Promise(function(pResolve, pReject){
setTimeout(function(){
console.log("p1");
pReject("nop p1 after 1s");
}, 1000);
});
console.log(pRequest.superbody);

let p2 = new Promise(function(pResolve, pReject){
setTimeout(function(){
console.log("p2");
pResolve("yes p2 after 2s");
}, 2000)
});
var f = new Form('test');

Promise.all([p1, p2]).then(function(v){
console.log("then", v);
}).catch(function(e){
console.log("catch e");
});
if(f.isValid()){
console.table(f.getValues());
}else{
let e = f.getError();
if(e){
console.log(e);
}
}

let o = {"test":function(){}, "foo":"bar", "num":12};
console.dir(o);
console.log("éèçé");
console.log("bouboup");
console.info("attention notice");
console.warn("attention warning");
console.error("attention error");
console.table([{a:"1", b:"1"}, {a:"0", b:"0", c:"0"}, {bouboup:42}, {bouboup:[1,2,3]}]);*/
var t = new Template('index.tpl');
t.assign('test', 'bouboup');
t.assign('form_test', f.render());
t.render(pResponse);
},
bouboup:function(pRequest, pResponse){
Expand Down
2 changes: 1 addition & 1 deletion src/shared/routes.js
@@ -1,6 +1,6 @@
module.exports = {
"/":{
"GET":"index.index"
"*":"index.index"
},
"/bouboup":{
"GET":"index.bouboup"
Expand Down
1 change: 1 addition & 0 deletions src/views/index.tpl
Expand Up @@ -2,4 +2,5 @@
<h1>Welcome /</h1>
<a href="/bouboup">bouboup</a>
<h2>{$test}</h2>
{$form_test}
{=include("includes/footer.tpl")}

0 comments on commit 1ecceeb

Please sign in to comment.