mma-ask-app/ask.js
2020-01-26 17:41:23 -08:00

53 lines
1.3 KiB
JavaScript

'use strict';
const http = require('http');
// ----------------------------------------------------------------------
const hostname = '0.0.0.0';
const port = 3000;
// ----------------------------------------------------------------------
var data = {
'canaarohihavecoffee': {
'target': Date.parse('2020-05-24'),
'question': "Can Aarohi have coffee"
}
};
// ----------------------------------------------------------------------
function getPage(req) {
var question = "Do I understand your query";
var answer = "No!";
var route = req.url.substring(1);
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>
<head>
<title>${question}?</title>
</head>
<body>
<h1>${question}?</h1>
<p>${answer}</p>
</body>
</html>`;
}
// ----------------------------------------------------------------------
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.write(getPage(req));
res.end();
console.log(`Serviced request ${req}`);
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});