Host from config if specified

This commit is contained in:
Klemek
2019-06-26 19:28:00 +02:00
parent 0fde428806
commit a7fedb149f
2 changed files with 12 additions and 7 deletions
+11 -7
View File
@@ -36,7 +36,7 @@ module.exports = (config) => {
const articles = {}; const articles = {};
let lastRSS = ''; let lastRSS = '';
let host; let host = config['host'];
/** /**
* Fetch articles from the data folder and send success as a response * Fetch articles from the data folder and send success as a response
@@ -67,17 +67,20 @@ module.exports = (config) => {
/** /**
* Render the page with the view engine and catch errors * Render the page with the view engine and catch errors
* @param req
* @param res * @param res
* @param vPath - path of the view * @param vPath - path of the view
* @param data - data to pass to the view * @param data - data to pass to the view
* @param code - code to send along the page * @param code - code to send along the page
*/ */
const render = (res, vPath, data, code = 200) => { const render = (req, res, vPath, data, code = 200) => {
data.info = { data.info = {
title: config['home']['title'], title: config['home']['title'],
description: config['home']['description'], description: config['home']['description'],
host: host, host: host,
version: pjson.version version: pjson.version,
request: req,
config: config
}; };
res.render(vPath, data, (err, html) => { res.render(vPath, data, (err, html) => {
if (err) { if (err) {
@@ -90,17 +93,18 @@ module.exports = (config) => {
/** /**
* Show an error with the correct page * Show an error with the correct page
* @param req
* @param resPath - the page of the original error * @param resPath - the page of the original error
* @param code - error code * @param code - error code
* @param res * @param res
*/ */
const showError = (resPath, code, res) => { const showError = (req, resPath, code, res) => {
const errorPath = path.join(config['data_dir'], config['home']['error']); const errorPath = path.join(config['data_dir'], config['home']['error']);
fs.access(errorPath, fs.constants.R_OK, (err) => { fs.access(errorPath, fs.constants.R_OK, (err) => {
if (err) if (err)
res.sendStatus(code); res.sendStatus(code);
else else
render(res, errorPath, {error: code, path: resPath}, code); render(req, res, errorPath, {error: code, path: resPath}, code);
}); });
}; };
@@ -135,7 +139,7 @@ module.exports = (config) => {
if (err) if (err)
showError(req.path, 404, res); showError(req.path, 404, res);
else else
render(res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))}); render(req, res, homePath, {articles: Object.values(articles).sort((a, b) => ('' + b.path).localeCompare(a.path))});
}); });
}); });
@@ -219,7 +223,7 @@ module.exports = (config) => {
console.log(cons.error, `no template found at ${templatePath}`); console.log(cons.error, `no template found at ${templatePath}`);
showError(req.path, 500, res); showError(req.path, 500, res);
} else } else
render(res, templatePath, {article: article}); render(req, res, templatePath, {article: article});
}); });
}); });
} }
+1
View File
@@ -1,5 +1,6 @@
{ {
"node_port": 3000, "node_port": 3000,
"host": "",
"data_dir": "data", "data_dir": "data",
"view_engine": "ejs", "view_engine": "ejs",
"access_log": "access.log", "access_log": "access.log",