-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (45 loc) · 1.53 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
"use strict";
const routing = require('./lib/routing');
const helpers = require('./lib/helpers');
const httpServer = require('./lib/http_server');
const AsyncEventEmitter = require('./lib/async_event_emitter');
class EscobarServer extends AsyncEventEmitter {
constructor() {
super();
this.host = '0.0.0.0';
this.port = 3000;
this.backlog = 65535;
this.routes = {};
// Parsers
this.useJsonParser = true;
this.useMultipartParser = true;
this.useUrlencodedParser = true;
}
async loadRoutes(pathToFolder) {
const timerStr = 'Routes load time';
console.time(timerStr);
const _routes = await routing(pathToFolder);
if (_routes === false) {
console.error(`There is no routes loaded, please check ${pathToFolder} folder.`);
} else {
console.timeEnd(timerStr);
this.routes = _routes;
helpers.forEach(_routes, (key) => {
console.log(`Loaded route: ${key}`);
});
}
};
startServer () {
/**
* Run HTTP server and listen connections
*/
this.httpServer = httpServer.apply(this);
this.httpServer.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
this.httpServer.listen(this.port, this.host, this.backlog, () => {
console.log(`Server running at http://${this.host}:${this.port}/`);
});
};
}
module.exports = EscobarServer;