-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
83 lines (67 loc) · 2.4 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers',
'Origin,X-Requested-With, Content-Type, Accept');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
function randomDelayInMS() {
// random delay in milliseconds between 0 to 10000 ms
return (Math.floor(Math.random() * 10) + 0) * 1000;
}
function timerFunction(res, data, type, delay) {
setTimeout(() => {
if (type === 'typeDelay') {
data.delay = `${delay} ms`;
res.send(data);
} else if (type === 'typeUrl') {
res.status(302).redirect(data);
}
}, delay);
}
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/public/index.html'));
});
app.get('/doc', (req, res) => {
res.sendFile(path.join(__dirname, '/public/doc.html'));
});
app.all('/delay/:delayValue/', (req, res) => {
const data = {
status: 200,
message: 'Mock response from Flash'
};
if (req.params.delayValue === 'random') {
(timerFunction(res, data, 'typeDelay', randomDelayInMS()));
} else if (!isNaN(req.params.delayValue)) {
(timerFunction(res, data, 'typeDelay', req.params.delayValue));
} else {
res.status(500).send('Delay value should be valid number(in milliseconds) or "random"');
}
});
app.get('/delay/:delayValue/url/:urlValue*', (req, res) => {
var url = `${req.params.urlValue}${req.params[0]}`;
if (!url.match('(http|https)://') && !url.match('://')) {
url = `http://${url}`;
}
if (req.params.delayValue === 'random') {
(timerFunction(res, url, 'typeUrl', randomDelayInMS()));
} else if (!isNaN(req.params.delayValue)) {
(timerFunction(res, url, 'typeUrl', req.params.delayValue));
} else {
res.status(500).send('Delay value should be valid number(in milliseconds) or "random"');
}
});
app.get('*', function (req, res) {
res.redirect('/');
});
app.set('port', process.env.PORT || 3000);
let server = app.listen(app.get('port'), () => {
// eslint-disable-next-line
console.log('Flash ⚡⚡⚡ is listening to port ' + app.get('port'));
});
module.exports = server;