Modularizing things. Beautification of output

This commit is contained in:
Mahesh Asolkar 2020-01-26 18:45:54 -08:00
parent 3644543ff0
commit 7382370b93
2 changed files with 57 additions and 18 deletions

46
ask.js
View File

@ -7,35 +7,45 @@ const hostname = '0.0.0.0';
const port = 3000; const port = 3000;
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
var data = { var logic = require('./logic.js');
'canaarohihavecoffee': {
'target': Date.parse('2020-05-24'),
'question': "Can Aarohi have coffee"
}
};
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
function getPage(req) { function getPage(req) {
var question = "Do I understand your query";
var answer = "No!";
var route = req.url.substring(1); var route = req.url.substring(1);
var query = logic.riddle_me(route);
console.log(`Serving route ${route}`); console.log(`Serving route ${route}`);
if (route in data) {
var scope = data[route];
question = scope.question;
answer = (Date.now() > scope.target) ? "Yes!" : "No!";
}
return `<html> return `<html>
<head> <head>
<title>${question}?</title> <title>${query.question}?</title>
<link href="https://fonts.googleapis.com/css?family=Bowlby+One+SC|Special+Elite&display=swap"
rel="stylesheet">
<style type="text/css">
.question {
font-family: 'Special Elite', cursive;
font-size: xx-large;
text-align: center;
margin-top: 200px;
}
.answer {
font-family: 'Bowlby One SC', cursive;
font-size: 200px;
text-align: center;
margin-top: 20px;
margin-bottom: 20px;
}
.yes {
color: green;
}
.no {
color: red;
}
</style>
</head> </head>
<body> <body>
<h1>${question}?</h1> <p class="question">${query.question}?</p>
<p>${answer}</p> <p class="answer ${query.answer}">${query.answer}!</p>
</body> </body>
</html>`; </html>`;
} }

29
logic.js Normal file
View File

@ -0,0 +1,29 @@
// Answers to questions
// --------------------
var data = {
'canaarohihavecoffee': {
'question': "Can Aarohi have coffee",
'logic': function () {
return (Date.now() > Date.parse('2020-05-24')) ? "yes" : "no";
}
}
};
// Exported functions
module.exports = {
riddle_me: function (q) {
var ret = {
"question": "Do I understand your query",
"answer": "no"
};
if (q in data) {
var scope = data[q];
ret.question = scope.question,
ret.answer = scope.logic()
}
return ret;
}
}