-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (58 loc) · 2.18 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const http = require('http'),
fs = require('fs'),
path = require('path'),
contentTypes = require('./utils/content-types'),
sysInfo = require('./utils/sys-info'),
env = process.env,
quotes = require('./data/quotes.json');
let server = http.createServer(function (req, res) {
let url = req.url;
if (url == '/') {
url += 'index.html';
}
// IMPORTANT: Your application HAS to respond to GET /health with status 200
// for OpenShift health monitoring
if (url == '/quote') {
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(quotes[Math.floor(Math.random() * quotes.length)]));
} else if (url.indexOf('/quote/') == 0) {
var character = url.split('/quote/')[1];
character= character.replace(/%20/g, " ");
var charArray = quotes.filter(function(quote) {
return quote.character.toUpperCase() === character.toUpperCase()
});
if(charArray.length === 0) {
res.writeHead(404);
res.end("No quote found");
}
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(charArray[Math.floor(Math.random() * charArray.length)]));
} else if (url == '/health') {
res.writeHead(200);
res.end();
} else if (url.indexOf('/info/') == 0) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(JSON.stringify(sysInfo[url.slice(6)]()));
} else {
fs.readFile('./static' + url, function (err, data) {
if (err) {
res.writeHead(404);
res.end();
} else {
let ext = path.extname(url).slice(1);
res.setHeader('Content-Type', contentTypes[ext]);
if (ext === 'html') {
res.setHeader('Cache-Control', 'no-cache, no-store');
}
res.end(data);
}
});
}
});
server.listen(env.NODE_PORT || 8080, env.NODE_IP || 'localhost', function () {
console.log('I am listening...')
console.log('Application worker ${process.pid} started...');
});