Browse Source

Implemented image request script and created 404 page

pull/4/head
jrtechs 7 years ago
parent
commit
12598ce9ef
13 changed files with 161 additions and 44 deletions
  1. +1
    -0
      README.md
  2. +10
    -0
      admin/addCategory.html
  3. +10
    -0
      admin/addCategory.js
  4. +1
    -1
      admin/admin.js
  5. +0
    -15
      admin/test.html
  6. +13
    -0
      img/image.js
  7. BIN
      img/website/404.jpg
  8. +2
    -0
      includes/404.html
  9. +18
    -1
      posts/posts.js
  10. +41
    -2
      posts/singlePost.js
  11. +31
    -16
      server.js
  12. +25
    -8
      utils/sql.js
  13. +9
    -1
      utils/utils.js

+ 1
- 0
README.md View File

@ -22,6 +22,7 @@ primary key(user_id)
create table categories( create table categories(
category_id mediumint unsigned not null AUTO_INCREMENT, category_id mediumint unsigned not null AUTO_INCREMENT,
name varchar(60) not null, name varchar(60) not null,
url varchar(60) not null,
primary key(category_id) primary key(category_id)
); );

+ 10
- 0
admin/addCategory.html View File

@ -0,0 +1,10 @@
<h1 class="w3-text-teal w3-center">Add Category</h1>
<form action="/admin/" method ="post" class="w3-container w3-card-4">
<div class="w3-group w3-padding-16">
<input class="w3-input" type="text" name="add_category" required>
<label class="w3-label w3-validate">Category</label>
</div>
<p><input type="submit" name="submit" value="Add"
class="w3-padding-16 w3-hover-dark-grey w3-btn-block w3-center-align"/></p>
</form>

+ 10
- 0
admin/addCategory.js View File

@ -0,0 +1,10 @@
const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
module.exports=
{
main: function(res)
{
utils.include(res, "./admin/addCategory.html");
}
};

+ 1
- 1
admin/admin.js View File

@ -2,6 +2,6 @@ module.exports=
{ {
main: function(res, fileName) main: function(res, fileName)
{ {
require("../admin/addCategory.js").main(res);
} }
}; };

+ 0
- 15
admin/test.html View File

@ -75,21 +75,6 @@ and is wrapped around the whole page content, except for the footer in this exam
tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla.</p> tellus ut rutrum. Sed vitae justo condimentum, porta lectus vitae, ultricies congue gravida diam non fringilla.</p>
<div class="w3-row">
<div class="w3-col m8 s12">
<p><button class="w3-button w3-padding-large w3-white w3-border"><b>READ MORE &raquo;</b></button></p>
</div>
<div class="w3-col m4 w3-hide-small">
<p><span class="w3-padding-large w3-right"><b>Comments &nbsp;</b> <span class="w3-tag">0</span></span></p>
</div>
</div>
</div> </div>

+ 13
- 0
img/image.js View File

@ -0,0 +1,13 @@
const utils = require('../utils/utils.js');
module.exports=
{
main: function(res, fileName)
{
path = "." + fileName;
res.contentType = 'image/png';
utils.include(res, path);
res.end();
}
};

BIN
img/website/404.jpg View File

Before After
Width: 600  |  Height: 564  |  Size: 35 KiB

+ 2
- 0
includes/404.html View File

@ -0,0 +1,2 @@
<h1 class="w3-center">Page Not Found</h1>
<center><img src="/img/website/404.jpg" alt="Page not found" width="70%" /></center>

+ 18
- 1
posts/posts.js View File

@ -1,5 +1,5 @@
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
const sql = require('../utils/sql');
module.exports= module.exports=
{ {
@ -11,7 +11,24 @@ module.exports=
*/ */
main: function(res, requestURL) main: function(res, requestURL)
{ {
res.write("<div class=\"w3-col l8 s12\">");
var splitURL = requestURL.split("/");
//user entered /category/name/ or /category/name
if(splitURL.length == 3 || splitURL.length == 4)
{
var post = sql.getPost(requestURL);
if(post)
require("../posts/singlePost.js").renderPost(res, post);
else
utils.include(res, "includes/404.html");
}
else
{
}
res.write("</div>");
require("../includes/sidebar.js").main(res, requestURL); require("../includes/sidebar.js").main(res, requestURL);
} }
}; };

+ 41
- 2
posts/singlePost.js View File

@ -1,7 +1,46 @@
const utils = require('../utils/utils.js');
module.exports= module.exports=
{ {
main: function(res, fileName)
renderPost: function(res, post)
{ {
res.write("<div class=\"w3-card-4 w3-margin w3-white\">");
//image
res.write("<div class=\"w3-container\">");
//title
res.write("<h3><b>" + post.name + "</b></h3>");
//date
res.write("<h5><span class=\"w3-opacity\">" + post.date + "</span></h5>");
res.write("</div>");
res.write("<div class=\"w3-container\">");
//include page content
utils.include(res, "../entries/" + post.url);
res.write("</div></div>")
} }
};
};
/*
<div class="w3-card-4 w3-margin w3-white">
<img src="/w3images/woods.jpg" alt="Nature" style="width:100%">
<div class="w3-container">
<h3><b>TITLE HEADING</b></h3>
<h5>Title description, <span class="w3-opacity">Date</span></h5>
</div>
<div class="w3-container">
content
</div>
</div>
*/

+ 31
- 16
server.js View File

@ -8,38 +8,53 @@
const http = require('http'); const http = require('http');
const url = require('url'); const url = require('url');
const includes = require('./includes/includes.js'); const includes = require('./includes/includes.js');
http.createServer(function (request_url, res) http.createServer(function (request_url, res)
{ {
var q = url.parse(request_url.url, true); var q = url.parse(request_url.url, true);
var filename = q.pathname; var filename = q.pathname;
console.log(filename.split("/"));
console.log(filename);
//prints header //prints header
includes.printHeader(res);
if(filename.includes("/category"))
{
//categories or view a category page
}
else if(filename.includes("/downloads/"))
//handles image requests
if(filename.includes("/img/"))
{ {
//downloads page
//probably will be implemented later
require("./img/image.js").main(res, filename);
} }
else if(filename.includes("/admin"))
else
{ {
includes.printHeader(res);
//categories or view a category page
if(filename.includes("/category"))
require("../posts/category.js").main(res, filename);
//downloads page
else if(filename.includes("/downloads/"))
{}
//admin page //admin page
else if(filename.includes("/admin"))
require("./admin/admin.js").main(res, filename);
}
else
{
//normal blog entry //normal blog entry
require("./posts/posts.js").main(res, filename);
else
require("./posts/posts.js").main(res, filename);
//includes footer file
includes.printFooter(res);
} }
//includes footer file
includes.printFooter(res);
}).listen(8080); }).listen(8080);

+ 25
- 8
utils/sql.js View File

@ -2,8 +2,9 @@ const mysql = require('mysql');
const con = mysql.createConnection({ const con = mysql.createConnection({
host: "localhost", host: "localhost",
user: "yourusername",
password: "yourpassword"
user: "blog_user",
password: "password",
database: "blog_name"
}); });
con.connect(function(err) { con.connect(function(err) {
@ -21,15 +22,13 @@ module.exports=
*/ */
fetch : function(sqlStatement) fetch : function(sqlStatement)
{ {
con.connect(function(err)
con.query(sqlStatement, function (err, result)
{ {
if (err) throw err; if (err) throw err;
con.query(sqlStatement, function (err, result)
{
if (err) throw err;
return result;
});
return result;
}); });
return []; return [];
}, },
/** /**
@ -50,5 +49,23 @@ module.exports=
}); });
}); });
return 0; return 0;
},
getPost : function(requestURL)
{
var splitURL = requestURL.split("/");
var result_category = this.fetch("select * from categories " +
"where url='" + splitURL[1] + "'");
if(result_category.length != 0)
{
var result_posts = this.fetch("select * from posts where" +
"category_id='" + result_category[0].category_id + "' " +
"and url='" + splitURL[2] + "'");
if(result_posts.length != 0)
{
return result_posts[0];
}
}
return 0;
} }
}; };

+ 9
- 1
utils/utils.js View File

@ -9,6 +9,14 @@ module.exports=
{ {
include: function(res, fileName) include: function(res, fileName)
{ {
res.write(fs.readFileSync(fileName));
try
{
res.write(fs.readFileSync(fileName));
}
catch (e)
{
console.log("Could not find " + fileName);
}
} }
}; };

Loading…
Cancel
Save