-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (63 loc) · 1.89 KB
/
app.js
File metadata and controls
71 lines (63 loc) · 1.89 KB
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
63
64
65
66
67
68
69
70
71
const http = require('http'),
fs = require('fs'),
path = require('path'),
contentTypes = require('./utils/content-types'),
sysInfo = require('./utils/sys-info'),
env = process.env;
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 == '/health') {
res.writeHead(200);
res.end();
} else {
url = trump(url, "?");
fs.readFile('.' + url, function (err, data) {
if (err) {
fs.readFile('./index.html', function (err, data) {
if (err) {
res.writeHead(404);
res.end('Not found');
} else {
let ext = 'html';
res.setHeader('Content-Type', contentTypes[ext]);
res.setHeader('Cache-Control', 'no-cache, no-store');
res.end(data);
}
});
} else {
let ext = path.extname(url).slice(1);
if (contentTypes[ext]) {
res.setHeader('Content-Type', contentTypes[ext]);
}
if (ext === 'html') {
res.setHeader('Cache-Control', 'no-cache, no-store');
}
res.end(data);
}
});
}
});
function trump(str, pattern) {
let trumped = ""; // default return for invalid string and pattern
if (str && str.length) {
trumped = str;
if (pattern && pattern.length) {
let idx = str.indexOf(pattern);
if (idx != -1) {
trumped = str.substring(0, idx);
}
}
}
return (trumped);
}
const nodePort = env.NODE_PORT || 3000;
const nodeIp = env.NODE_IP || 'localhost';
server.listen(nodePort, nodeIp, function () {
console.log(`Application worker ${process.pid} started...`);
console.log(`IP: ${nodeIp} PORT: ${nodePort}`);
});