-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
65 lines (56 loc) · 1.37 KB
/
index.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
const http = require('http')
const url = require('url')
const config = require('./config.js')
const writeHeaders = (res) => {
res.setHeader('Content-Type', 'application/json')
res.setHeader('X-Made-by', 'bullgit')
res.writeHead(200, {'Content-Type': 'application/json'})
return res
}
/**
* randomBool
* Retuns a random boolean based on some good math. Trust me!
*/
const randomBool = (min, max) => {
avg = max / 2
return !((Math.floor(Math.random() * (max - min + 1)) + min) > avg)
}
/*
* The HTTP Server. It returns the default home JSON for
* every request expect the following
* /true returns true
* /false returns false
* /random returns a random boolean
*/
const server = http.createServer((req, res) => {
let p = url.parse(req.url)
let r = writeHeaders(res)
let data = null
// Remove slashes
let clean_req_path = p.path.replace(/\//g, "")
// Switch through the request path
switch(clean_req_path) {
case "true":
data = {"boolean": true}
break
case "false":
data = {"boolean": false}
break
case "random":
data = {"boolean": randomBool(42, 420) }
break
default:
data = {
"baas": "boolean as a service",
"endpoints": {
"/true": "returns true",
"/false": "returns false",
"/random": "returns random boolean"
},
"boolean": "none",
}
}
// Sent the JSON
r.end(JSON.stringify(data))
});
server.listen(config.port);