forked from sodocan/sodocan.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
133 lines (121 loc) · 2.66 KB
/
serve.js
File metadata and controls
133 lines (121 loc) · 2.66 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var http = require('http');
var fs = require('fs');
var PORT=4000;
var DIR=__dirname+'/blueprints';
var TEMPLATE = 'angular-sodone';
var headers = {'Access-Control-Allow-Origin':'*'};
var countfor = {};
countfor['thing2:method0'] = 1;
countfor['thing2:method1'] = -1;
var exampleJSON = [
{
"_id": "5625376402941ab4141fa4cb",
"project": "testProj",
"functionName": "method1",
"group": "testGroup",
"reference": {
"params": [],
"returns": []
},
"explanations": {
"descriptions": [
{
"text": "descriptionblahblah",
"upvotes": 0,
"additions": [],
"entryID": 1294058334
}
],
"examples": [
{
"text": "examplesblahblah",
"upvotes": 0,
"additions": [],
"entryID": 719765163
}
],
"tips": [
{
"text": "tipsblahblah",
"upvotes": 0,
"additions": [],
"entryID": 907176294
}
]
},
"__v": 0
},
{
"_id": "23487u928323",
"project": "testProj",
"functionName": "method2",
"group": "testGroup",
"reference": {
"params": [],
"returns": []
},
"explanations": {
"descriptions": [
{
"text": "descriptionblahblah",
"upvotes": 0,
"additions": [],
"entryID": 1294058334
}
],
"examples": [
{
"text": "examplesblahblah",
"upvotes": 0,
"additions": [],
"entryID": 719765163
}
],
"tips": [
{
"text": "tipsblahblah",
"upvotes": 0,
"additions": [],
"entryID": 907176294
}
]
},
"__v": 0
},
];
function handleRequest(req,res) {
if (req.url.match(/^\/api\/testProj/)) {
if (req.url.match(/testProj$/)) {
var ret = JSON.stringify(exampleJSON);
}
res.writeHead(200,headers);
res.end(ret);
}
var filePath = DIR+req.url;
var pos = filePath.search(/fakeUnderscore/);
if (pos>-1) {
var path = filePath.substr(pos+14);
filePath = DIR+'/'+path;
};
if (!/(html|js|css|png)$/.test(req.url)) {
filePath = DIR+'/'+TEMPLATE+'/index.html';
}
if(/favicon\.ico$/.test(req.url)) {
res.writeHead(200);
res.end('');
}
fs.readFile(filePath, function(err, data) {
if (err) {
console.log(filePath);
// every error returns 500 - oh well
res.writeHead(500);
return res.end('Error loading');
}
res.writeHead(200);
res.end(data);
});
}
var server = http.createServer(handleRequest);
server.listen(PORT,function() {
console.log('Server listening...');
});