-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (46 loc) · 1.65 KB
/
server.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
var express = require('express'),
xml2js = require('xml2js'),
fs = require('fs');
var app = new express(),
port = 8080;
var parser = new xml2js.Parser();
app.get('/:course/:lesson', function(req, res) {
var path = __dirname + '/xml/' + req.params.course + '/lesson' + req.params.lesson + '.xml';
fs.readFile(path, function(err, data) {
if (err !== null) {
return res.send('no result');
}
var quizJson = [],
aQuiz = {};
parser.parseString(data, function(err, result) {
for (var i = result.project.file.length - 1; i >= 0; i--) {
var obj = result.project.file[i],
name = obj.path[0],
content = obj.content[0];
var title = name.substr(0, name.length - 5),
type = name.substr(-5, 5);
if (typeof aQuiz[title] === 'undefined') {
aQuiz[title] = {};
aQuiz[title].title = title;
}
if (type === '.cond') {
aQuiz[title].stdout = content;
}
else if (type === '.html') {
aQuiz[title].description = content;
}
else if (type === '.java') {
aQuiz[title].ans = content;
}
else if (type === '.part') {
aQuiz[title].part = content;
}
}
for (var title in aQuiz) {
quizJson.push(aQuiz[title]);
}
res.send(quizJson);
});
});
});
app.listen(port);