forked from jsbuzz/dev-challenge-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevChallenge.js
More file actions
79 lines (70 loc) · 2.53 KB
/
devChallenge.js
File metadata and controls
79 lines (70 loc) · 2.53 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
"use strict";
// var config = require(config);
var express = require('express');
var bodyParser = require('body-parser');
var endPoints = require('./endPoints');
module.exports = {
spinUp : function(config, _app) {
let app = _app || express();
// config defaults
config.taskRemote || (config.taskRemote = '/challenge');
config.taskRoot || (config.taskRoot = 'challenge');
config.apiRoot || (config.apiRoot = '');
// session
if(config.withSession) {
let session = require('express-session');
app.use(session({
secret: 'what3v3R',
resave: false,
saveUninitialized: true
}));
}
// challenges
for(let task of config.tasks) {
app.use(`${config.taskRemote}/${task}.js` , express.static(`${config.taskRoot}/${task}/front.js`));
app.get(`${config.taskRemote}/${task}/data.js` , function(req, res) {
try {
endPoints.data(config, task, req, res);
} catch(exception) {
if(config.onDataError) {
config.onDataError(exception, req, res);
} else {
console.error(exception);
if(exception.code && exception.code == 'MODULE_NOT_FOUND') {
res.sendStatus(404);
} else {
res.sendStatus(500);
}
}
}
});
}
// routing
let urlencodedParser = bodyParser.urlencoded({ extended: true });
app.post(`${config.apiRoot}/check`, urlencodedParser, function (req, res) {
try {
endPoints.check(config, req, res);
} catch(exception) {
if(config.onError) {
config.onError(exception, req, res);
} else {
console.error(exception);
res.sendStatus(500);
}
}
});
app.get(`${config.apiRoot}/jumpTo`, function(req, res) {
try {
endPoints.jumpTo(config, req, res);
} catch(exception) {
if(config.onError) {
config.onError(exception, req, res);
} else {
console.error(exception);
res.sendStatus(500);
}
}
});
return app;
}
};