Browse Source

Worked on the categories sidebar and cleaning up promise statements.

pull/4/head
jrtechs 7 years ago
parent
commit
3e6b0a7808
8 changed files with 113 additions and 182 deletions
  1. +13
    -45
      admin/admin.js
  2. +9
    -6
      img/image.js
  3. +28
    -8
      includes/includes.js
  4. +1
    -11
      posts/category.js
  5. +11
    -54
      server.js
  6. +24
    -39
      sidebar/categoriesSideBar.js
  7. +0
    -1
      utils/sql.js
  8. +27
    -18
      utils/utils.js

+ 13
- 45
admin/admin.js View File

@ -1,58 +1,26 @@
const utils = require('../utils/utils.js');
var Promise = require('promise');
module.exports=
{
/**
* Method calls the admin widgets it correct order
*
* @param result
* @param fileName
* @param request
* @return {*|Promise}
*/
main: function(result, fileName, request)
{
return new Promise(function(resolve, reject){
var promiseToGetPost = function(req)
{
return new Promise(function(resolve, reject)
{
if(req.method == 'POST')
{
var body = '';
req.on('data', function (data)
{
body += data;
//Kills request, don't steal my RAM!!
//You can only download so much ram ;)
if (body.length > 1e6)
{
req.connection.destroy();
reject();
}
});
req.on('end', function ()
{
console.log(body);
resolve(body);
});
}
else
{
resolve(0);
}
});
};
var promiseToDisplayContents = function(postData)
{
return require("../admin/addCategory.js").main(result, postData);
};
promiseToGetPost(request).then(function (postData)
return new Promise(function(resolve, reject)
{
utils.getPostData(request).then(function (postData)
{
return promiseToDisplayContents(postData);
return require("../admin/addCategory.js").main(result, postData);
}).then(function()
{
console.log("admin page ended");
resolve();
});
});

+ 9
- 6
img/image.js View File

@ -2,12 +2,15 @@ const utils = require('../utils/utils.js');
module.exports=
{
main: function(res, fileName)
/**Sends the user an image from the specified fileName.
*
* @param result
* @param fileName
*/
main: function(result, fileName)
{
path = "." + fileName;
res.contentType = 'image/png';
utils.include(res, path);
res.end();
result.contentType = 'image/png';
utils.include(result, "." + fileName);
result.end();
}
};

+ 28
- 8
includes/includes.js View File

@ -8,18 +8,38 @@ const HEADER_FILE = "includes/header.html";
const FOOTER_FILE = "includes/footer.html";
const Promise = require('promise');
module.exports =
{
printHeader: function(res)
/** Appends the header html section to the result which is
* sent to the user.
*
* @param result
* @return {*} a promise retrieved from the utils.include function
*/
printHeader: function(result)
{
res.writeHead(200, {'Content-Type': 'text/html'});
utils.include(res, HEADER_FILE);
return 0;
result.writeHead(200, {'Content-Type': 'text/html'});
return utils.include(result, HEADER_FILE);
},
printFooter: function(res)
/**
* Appends the footer to the result object
*
* @param result
* @return {*|Promise}
*/
printFooter: function(result)
{
utils.include(res, FOOTER_FILE);
res.end();
return 0;
return new Promise(function(resolve, reject)
{
console.log(FOOTER_FILE);
utils.include(result, FOOTER_FILE).then(function()
{
result.end();
resolve();
})
})
}
};

+ 1
- 11
posts/category.js View File

@ -7,17 +7,7 @@ module.exports=
{
return new Promise(function(resolve, reject)
{
sql.getCategories().then(function(categories)
{
console.log(categories);
for(var category in categories)
{
res.write(category);
}
}).then(function())
{
resolve();
}
resolve();
});
}
};

+ 11
- 54
server.js View File

@ -11,8 +11,6 @@ const url = require('url');
const includes = require('./includes/includes.js');
var Promise = require('promise');
http.createServer(function (request, res)
{
var q = url.parse(request.url, true);
@ -27,66 +25,25 @@ http.createServer(function (request, res)
{
var file = "";
if(filename.includes("/category"))
{
//categories or view a category page
if(filename.includes("/category")) //single category page
file = "../posts/category.js";
}
else if(filename.includes("/admin"))
{
//admin page
else if(filename.includes("/admin")) //top secret admin page
file = "./admin/admin.js";
}
else
{
//normal blog entry
else //single post page
file = "./posts/posts.js";
}
var displayHeader = function()
{
return new Promise(function(resolve, reject)
{
var status = includes.printHeader(res);
if(status == 0)
{
console.log("Header done");
resolve();
}
});
};
var displayContent = function()
includes.printHeader(res).then(function()
{
return new Promise(function(resolve, reject)
{
require(file).main(res, filename, request).then(function()
{
resolve();
});
});
};
var displayFooter = function()
return require(file).main(res, filename, request);
}).then(function()
{
return new Promise(function(resolve, reject)
{
var status = includes.printFooter(res);
if(status == 0)
{
console.log("Footer done");
resolve();
}
});
};
displayHeader().then(function()
return includes.printFooter(res);
}).then(function()
{
return displayContent();
}).then(function(){
return displayFooter()
}).then(function(){
console.log("finished!!!!!!!!!!!!!!!")
console.log("fin"); //for debugging
})
}
}).listen(8080);

+ 24
- 39
sidebar/categoriesSideBar.js View File

@ -14,49 +14,34 @@ module.exports=
{
return new Promise(function(resolve, reject)
{
res.write("<div class=\"w3-col l4\">");
res.write("<div class=\"w3-card w3-margin w3-margin-top\">");
res.write(" <div class=\"w3-container w3-padding\">\n" +
"\n" +
" <h3>Categories</h3>\n" +
"\n" +
" </div>");
res.write("<div class=\"w3-container w3-white\">");
res.write("<ol>");
sql.getCategories().then(function(categories)
{
console.log(categories[0].name);
console.log("cool beans");
// for(var category in categories)
// {
// console.log(category);
// res.write(category.name);
// }
res.write("</div>");
categories.forEach(function(cat)
{
//res.write(cat.name);
console.log(cat);
res.write("<a href=\"#\#\" class=\"w3-bar-item w3-button\" style=\"width=100%\">" + cat.name + "</a>");
res.write("<br />");
});
res.write("</ol>");
res.write("</div></div>");
resolve();
})
});
}
};
/*
<!-- Labels / tags -->
<div class="w3-card w3-margin">
<div class="w3-container w3-padding">
<h4>Tags</h4>
</div>
<div class="w3-container w3-white">
<p><span class="w3-tag w3-black w3-margin-bottom">Travel</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">New York</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">London</span>
<span class="w3-tag w3-light-grey w3-small w3-margin-bottom">IKEA</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">NORWAY</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">DIY</span>
<span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Ideas</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Baby</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Family</span>
<span class="w3-tag w3-light-grey w3-small w3-margin-bottom">News</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Clothing</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Shopping</span>
<span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Sports</span> <span class="w3-tag w3-light-grey w3-small w3-margin-bottom">Games</span>
</p>
</div>
</div>
*/
};

+ 0
- 1
utils/sql.js View File

@ -27,7 +27,6 @@ var fetch = function(sqlStatement)
console.log("sql fetch method called");
return new Promise(function(resolve, reject)
{
con.query(sqlStatement, function (err, result)
{
if (err)

+ 27
- 18
utils/utils.js View File

@ -38,29 +38,38 @@ module.exports=
* @param request sent by user in initial server call
* @return the post data
*/
getPostData: function(request)
getPostData: function(req)
{
console.log("Get post data method");
if(request.method == 'POST')
return new Promise(function(resolve, reject)
{
var body = '';
request.on('data', function (data)
if(req.method == 'POST')
{
body += data;
var body = '';
req.on('data', function (data)
{
body += data;
//Kills request, don't steal my RAM!!
//You can only download so much ram ;)
if (body.length > 1e6)
{
req.connection.destroy();
reject();
}
//Kills request, don't steal my RAM!!
//You can only download so much ram ;)
if (body.length > 1e6)
request.connection.destroy();
});
});
request.on('end', function ()
req.on('end', function ()
{
console.log(body);
resolve(body);
});
}
else
{
console.log(body);
return body;
});
}
return {};
resolve(0);
}
});
}
};

Loading…
Cancel
Save