-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (42 loc) · 1.41 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
var koa = require("koa");
var serve = require("koa-static");
var path = require('path');
var config = require("./config");
var server = koa();
server.use(serve(path.join(__dirname, 'dist')));
server.use(function*(next) {
try {
yield next;
} catch (err){
console.log('from serverjs',err);
this.status = err.status || 500;
if(typeof(err.errors) === 'string' || typeof(err.errors) === 'undefined')
this.body = [
{
"type" : "custom",
"message" : err.errors ? err.errors : err.message,
"rule" : "custom"
}
];
else {
let errorBody = [];
for (var error of err.errors) {
let errorField = Object.keys(error)[0];
errorBody.push({
"type" : error[errorField]["type"],
"field" : errorField,
"message" : errorConf[error[errorField]["message"]],
"rule" : error[errorField]["rule"],
"code" : parseInt(error[errorField]["message"],10)
});
}
this.body = errorBody;
}
}
});
server.listen(config.serverPort, (err) => {
if(err) {
console.error("Server Error: " + err);
}
console.info('==> ✅ Server is running on port: ' + config.serverPort);
});