Browse Source

Got categories page working

pull/4/head
jrtechs 6 years ago
parent
commit
69765acfab
8 changed files with 95 additions and 41 deletions
  1. +7
    -28
      entries/testing-my-server.md
  2. +3
    -3
      includes/header.html
  3. +52
    -4
      posts/category.js
  4. +2
    -1
      posts/posts.js
  5. +2
    -3
      posts/singlePost.js
  6. +4
    -2
      server.js
  7. +20
    -0
      utils/sql.js
  8. +5
    -0
      utils/utils.js

+ 7
- 28
entries/testing-my-server.md View File

@ -3,33 +3,12 @@
This page is merely for testing -- go away
```
include <stdio.h>
void main(int argc, char * argv[])
{
int vals[1000];
//value will overflow a 32 bit int!!!
long int max = 0;
char * data = argv[1];
for(int i = 0; i < 1000; i++)
{
vals[i] = (int)data[i] - 48;
}
int i = 0;
```
for(int i = 0; i < 1000 - 13; i ++)
{
long int tempMax = 1;
for(int t = i; t < i + 13; t++)
{
tempMax *= vals[t];
}
if(tempMax > max)
{
max = tempMax;
}
}
##This is a h2
printf("the max number is %ld", max);
}
```
```
python3 roosay.py
```
This is just to test what a paragraph looks like.

+ 3
- 3
includes/header.html View File

@ -28,9 +28,9 @@
<!-- Navbar -->
<div class="w3-top">
<div class="w3-bar w3-dark-grey w3-card">
<a href="" class="w3-bar-item w3-button w3-padding-large">Home</a>
<a href="about" class="w3-bar-item w3-button w3-padding-large w3-hide-small">About</a>
<a href="categories" class="w3-bar-item w3-button w3-padding-large w3-hide-small">Categories</a>
<a href="/" class="w3-bar-item w3-button w3-padding-large">Home</a>
<a href="/about" class="w3-bar-item w3-button w3-padding-large w3-hide-small">About</a>
<a href="/categories" class="w3-bar-item w3-button w3-padding-large w3-hide-small">Categories</a>
</div>
</div>

+ 52
- 4
posts/category.js View File

@ -1,13 +1,61 @@
var Promise = require('promise');
const sql = require('../utils/sql');
const utils = require('../utils/utils.js');
module.exports=
var renderPosts = function(result, resultURL)
{
main: function(res, fileName)
var splitURL = resultURL.split("/");
if(splitURL.length >= 3)
{
result.write("<div class=\"w3-col l8 s12\">\n");
return new Promise(function(resolve, reject)
{
resolve();
sql.getPostsFromCategory(splitURL[2]).then(function(posts)
{
posts.forEach(function(p)
{
require("../posts/singlePost.js").renderPost(result, p);
});
}).then(function()
{
result.write("</div>");
resolve();
})
});
}
};
else
{
return utils.print404(result);
}
};
module.exports=
{
renderPostPreview: function(result, postSQLData)
{
},
/**
* Calls posts and sidebar modules to render blog contents in order
*
* @param res
* @param fileName request url
*/
main: function(res, requestURL, request)
{
console.log("category page");
return new Promise(function(resolve, reject)
{
renderPosts(res, requestURL).then(function()
{
return require("../sidebar/sidebar.js").main(res)
}).then(function ()
{
resolve();
})
});
}
}

+ 2
- 1
posts/posts.js View File

@ -29,7 +29,7 @@ var renderPost = function(res, requestURL)
}
else
{
return utils.include(res, "includes/404.html");
return utils.print404(res);
}
}).then(function()
{
@ -54,6 +54,7 @@ module.exports=
*/
main: function(res, requestURL, request)
{
console.log("posts page");
return new Promise(function(resolve, reject)
{
renderPost(res, requestURL).then(function()

+ 2
- 3
posts/singlePost.js View File

@ -4,9 +4,6 @@ var Promise = require('promise');
var markdown = require( "markdown" ).markdown;
var Markdown = require('markdown-to-html').Markdown;
var md = new Markdown();
module.exports=
{
@ -23,6 +20,8 @@ module.exports=
{
res.write("<div class=\"w3-card-4 w3-margin w3-white\">");
//image
res.write("<img src=\"https://www.w3schools.com/w3images/woods.jpg\" alt=\"Nature\" style=\"width:100%\">");
res.write("<div class=\"w3-container\">");
//title
res.write("<h3><b>" + post.name + "</b></h3>");

+ 4
- 2
server.js View File

@ -29,8 +29,8 @@ http.createServer(function (request, res)
{
var file = "";
if(filename.includes("/category")) //single category page
file = "../posts/category.js";
if(filename.includes("/categories/")) //single category page
file = "./posts/category.js";
else if(filename.includes("/admin")) //top secret admin page
file = "./admin/admin.js";
@ -38,6 +38,8 @@ http.createServer(function (request, res)
else //single post page
file = "./posts/posts.js";
console.log(file);
includes.printHeader(res).then(function()
{
return require(file).main(res, filename, request);

+ 20
- 0
utils/sql.js View File

@ -115,5 +115,25 @@ module.exports=
{
var q = "select * from categories";
return fetch(q);
},
getPostsFromCategory: function(requestURL)
{
return new Promise(function(resolve, reject)
{
var q = "select * from categories where name ='" + requestURL + "' limit 1";
fetch(q).then(function(categories)
{
if(categories.length != 0)
{
var qPosts = "select * from posts where category_id='" + categories[0].category_id + "'";
resolve(fetch(qPosts));
}
else
{
resolve(0);
}
});
});
}
};

+ 5
- 0
utils/utils.js View File

@ -85,5 +85,10 @@ module.exports=
resolve(0);
}
});
},
print404: function(result)
{
return this.include(result, "includes/404.html");
}
};

Loading…
Cancel
Save