Browse Source

So many Promises were added to deal with sync.

pull/4/head
jrtechs 7 years ago
parent
commit
57e22e445f
9 changed files with 213 additions and 48 deletions
  1. +2
    -0
      README.md
  2. +31
    -1
      admin/addCategory.js
  3. +55
    -2
      admin/admin.js
  4. +2
    -0
      includes/includes.js
  5. +2
    -5
      includes/sidebar.html
  6. +1
    -1
      posts/posts.js
  7. +62
    -29
      server.js
  8. +17
    -8
      utils/sql.js
  9. +41
    -2
      utils/utils.js

+ 2
- 0
README.md View File

@ -50,4 +50,6 @@ grant all on blog_name.* to blog_user@localhost identified by "password";
## Node Dependencies ## Node Dependencies
```bash ```bash
npm install mysql npm install mysql
npm install sanitizer
npm install promise
``` ```

+ 31
- 1
admin/addCategory.js View File

@ -1,10 +1,40 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
const sql = require('../utils/sql'); const sql = require('../utils/sql');
const qs = require('querystring');
var Promise = require('promise');
module.exports= module.exports=
{ {
main: function(res)
main: function(res, postData)
{ {
utils.include(res, "./admin/addCategory.html"); utils.include(res, "./admin/addCategory.html");
return this.processPost(res, postData);
},
processPost: function(res, postData)
{
return new Promise(function(resolve, reject)
{
var post = qs.parse(postData);
if(post.add_category)
{
var url = post.add_category.replace(/ /i, "-");
var q = "insert into categories (name, url) values " +
"('" + post.add_category + "','" + url + "')";
console.log(q);
if(sql.insert(q) != 0)
{
console.log("category added");
}
else
{
console.log("error adding category");
}
}
resolve();
});
} }
}; };

+ 55
- 2
admin/admin.js View File

@ -1,7 +1,60 @@
const utils = require('../utils/utils.js');
var Promise = require('promise');
module.exports= module.exports=
{ {
main: function(res, fileName)
main: function(result, fileName, request)
{ {
require("../admin/addCategory.js").main(res);
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 promiseToDisplayContents(postData);
}).then(function()
{
resolve();
});
});
} }
}; };

+ 2
- 0
includes/includes.js View File

@ -14,10 +14,12 @@ module.exports =
{ {
res.writeHead(200, {'Content-Type': 'text/html'}); res.writeHead(200, {'Content-Type': 'text/html'});
utils.include(res, HEADER_FILE); utils.include(res, HEADER_FILE);
return 0;
}, },
printFooter: function(res) printFooter: function(res)
{ {
utils.include(res, FOOTER_FILE); utils.include(res, FOOTER_FILE);
res.end(); res.end();
return 0;
} }
}; };

+ 2
- 5
includes/sidebar.html View File

@ -5,7 +5,7 @@
<div class="w3-card w3-margin w3-margin-top"> <div class="w3-card w3-margin w3-margin-top">
<img src="/w3images/avatar_g.jpg" style="width:100%">
<div class="w3-container w3-white"> <div class="w3-container w3-white">
@ -33,7 +33,6 @@
<li class="w3-padding-16"> <li class="w3-padding-16">
<img src="/w3images/workshop.jpg" alt="Image" class="w3-left w3-margin-right" style="width:50px">
<span class="w3-large">Lorem</span><br> <span class="w3-large">Lorem</span><br>
@ -43,7 +42,6 @@
<li class="w3-padding-16"> <li class="w3-padding-16">
<img src="/w3images/gondol.jpg" alt="Image" class="w3-left w3-margin-right" style="width:50px">
<span class="w3-large">Ipsum</span><br> <span class="w3-large">Ipsum</span><br>
@ -53,7 +51,6 @@
<li class="w3-padding-16"> <li class="w3-padding-16">
<img src="/w3images/skies.jpg" alt="Image" class="w3-left w3-margin-right" style="width:50px">
<span class="w3-large">Dorum</span><br> <span class="w3-large">Dorum</span><br>
@ -63,7 +60,7 @@
<li class="w3-padding-16 w3-hide-medium w3-hide-small"> <li class="w3-padding-16 w3-hide-medium w3-hide-small">
<img src="/w3images/rock.jpg" alt="Image" class="w3-left w3-margin-right" style="width:50px">
<span class="w3-large">Mingsum</span><br> <span class="w3-large">Mingsum</span><br>

+ 1
- 1
posts/posts.js View File

@ -9,7 +9,7 @@ module.exports=
* @param res * @param res
* @param fileName request url * @param fileName request url
*/ */
main: function(res, requestURL)
main: function(res, requestURL, request)
{ {
res.write("<div class=\"w3-col l8 s12\">"); res.write("<div class=\"w3-col l8 s12\">");

+ 62
- 29
server.js View File

@ -11,16 +11,12 @@ const url = require('url');
const includes = require('./includes/includes.js'); const includes = require('./includes/includes.js');
http.createServer(function (request_url, res)
var Promise = require('promise');
http.createServer(function (request, res)
{ {
var q = url.parse(request_url.url, true);
var q = url.parse(request.url, true);
var filename = q.pathname; var filename = q.pathname;
console.log(filename.split("/"));
console.log(filename);
//prints header
//handles image requests //handles image requests
if(filename.includes("/img/")) if(filename.includes("/img/"))
@ -29,32 +25,69 @@ http.createServer(function (request_url, res)
} }
else else
{ {
includes.printHeader(res);
var file = "";
//categories or view a category page
if(filename.includes("/category")) if(filename.includes("/category"))
require("../posts/category.js").main(res, filename);
//downloads page
else if(filename.includes("/downloads/"))
{}
//admin page
{
//categories or view a category page
file = "../posts/category.js";
}
else if(filename.includes("/admin")) else if(filename.includes("/admin"))
require("./admin/admin.js").main(res, filename);
//normal blog entry
{
//admin page
file = "./admin/admin.js";
}
else else
require("./posts/posts.js").main(res, filename);
{
//normal blog entry
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()
{
return new Promise(function(resolve, reject)
{
require(file).main(res, filename, request).then(function(){
resolve();
});
});
};
var displayFooter = function()
{
return new Promise(function(resolve, reject)
{
var status = includes.printFooter(res);
if(status == 0)
{
console.log("Footer done");
resolve();
}
});
};
displayHeader().then(function()
{
return displayContent();
}).then(function(){
return displayFooter()
}).then(function(){
console.log("finished!!!!!!!!!!!!!!!")
})
//includes footer file
includes.printFooter(res);
} }
}).listen(8080); }).listen(8080);

+ 17
- 8
utils/sql.js View File

@ -1,5 +1,7 @@
const mysql = require('mysql'); const mysql = require('mysql');
const sanitizer = require('sanitizer');
const con = mysql.createConnection({ const con = mysql.createConnection({
host: "localhost", host: "localhost",
user: "blog_user", user: "blog_user",
@ -22,7 +24,6 @@ module.exports=
*/ */
fetch : function(sqlStatement) fetch : function(sqlStatement)
{ {
con.query(sqlStatement, function (err, result) con.query(sqlStatement, function (err, result)
{ {
if (err) throw err; if (err) throw err;
@ -34,23 +35,31 @@ module.exports=
/** /**
* Function used to use insert statements into the database * Function used to use insert statements into the database
* *
* Don't worry, the input gets sanitized
*
* @param sqlStatement * @param sqlStatement
* @return the id of the new record - if there is one * @return the id of the new record - if there is one
*/ */
insert : function(sqlStatement) insert : function(sqlStatement)
{ {
con.connect(function(err)
con.query(sanitizer.sanitize(sqlStatement), function (err, result)
{ {
if (err) throw err;
con.query(sqlStatement, function (err, result)
if (err)
{ {
if (err) throw err;
return result.insertId;
});
console.log(err);
return 0;
}
return result.insertId;
}); });
return 0;
}, },
/**
* Not to be mistaken for getPostData() in @file utils/utils.js,
* this function extracts a post entry from the sql server
*
* @param requestURL url user used to request blog post
* @return {*} the entry found in the data base -- if any
*/
getPost : function(requestURL) getPost : function(requestURL)
{ {
var splitURL = requestURL.split("/"); var splitURL = requestURL.split("/");

+ 41
- 2
utils/utils.js View File

@ -7,16 +7,55 @@ const fs = require('fs');
module.exports= module.exports=
{ {
include: function(res, fileName)
/**
* A function similar to the include statement in PHP
* This function writes a file to the output
*
* @param result the result that is sent to the user from node
* @param fileName the file to append to the result
*/
include: function(result, fileName)
{ {
try try
{ {
res.write(fs.readFileSync(fileName));
result.write(fs.readFileSync(fileName));
} }
catch (e) catch (e)
{ {
console.log("Could not find " + fileName); console.log("Could not find " + fileName);
} }
},
/**
* Function which is responsible for returning all post data.
*
* @param request sent by user in initial server call
* @return the post data
*/
getPostData: function(request)
{
console.log("Get post data method");
if(request.method == 'POST')
{
var body = '';
request.on('data', function (data)
{
body += data;
//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 ()
{
console.log(body);
return body;
});
}
return {};
} }
}; };

Loading…
Cancel
Save