Browse Source

Spent multiple hours learning nodejs and how synchronous calls work. Currently the server includes a header and footer file.

pull/4/head
BuildTools 6 years ago
parent
commit
a931115795
4 changed files with 74 additions and 0 deletions
  1. +0
    -0
      category.js
  2. +25
    -0
      includes.js
  3. +35
    -0
      server.js
  4. +14
    -0
      utils.js

+ 0
- 0
category.js View File


+ 25
- 0
includes.js View File

@ -0,0 +1,25 @@
/**
Includes.js
File used for getting the header and footer
*/
const utils = require('./utils.js');
const HEADER_FILE = "includes/header.html";
const FOOTER_FILE = "includes/footer.html";
module.exports =
{
printHeader: function(res)
{
res.writeHead(200, {'Content-Type': 'text/html'});
utils.include(res, HEADER_FILE);
//res.write("Header<br>");
},
printFooter: function(res)
{
//res.write("footer");
utils.include(res, FOOTER_FILE);
res.end();
}
};

+ 35
- 0
server.js View File

@ -0,0 +1,35 @@
var http = require('http');
const url = require('url');
const fs = require('fs');
const utils = require('./utils.js');
const includes = require('./includes.js');
http.createServer(function (req, res)
{
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
//prints header
includes.printHeader(res);
utils.include(res, "README.md");
if(filename.includes("/category"))
{
console.log("categories");
test();
}
else if(filename.includes("/posts/"))
{
console.log("posts");
}
else if(filename.includes("/downloads/"))
{
console.log("downloads");
}
else
{
console.log("default");
}
includes.printFooter(res);
}).listen(8080);

+ 14
- 0
utils.js View File

@ -0,0 +1,14 @@
/**
Utilities is a node modules created to make tasks like
including html files easier for me programming.
*/
const fs = require('fs');
module.exports=
{
include: function(res, fileName)
{
res.write(fs.readFileSync(fileName));
}
};

Loading…
Cancel
Save