-
Notifications
You must be signed in to change notification settings - Fork 7
/
base_service.js
50 lines (43 loc) · 1.21 KB
/
base_service.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
const express = require('express');
const config = require('./config');
const LogUtil = require('./log');
class BaseService {
constructor() {
this.app = express();
}
startServer() {
this.app.use("/", (req, res) => {
this.handleRequest(req, res);
});
this.app.use((err, req, res, next) => {
LogUtil.error(err);
res.status(500).send('Internal Server Error');
});
let hostname = config.ConfigManager.getInstance().getValue(config.keys.KEY_BIND_HOSTNAME);
if (!hostname) {
hostname = '0.0.0.0';
}
const port = this.getServerPort();
this.app.listen(port, hostname, (err) => {
if (err) {
LogUtil.error(err);
} else {
LogUtil.info(`${this.getServiceName()} service has been started, port: ${port}`);
}
});
}
getServiceName() {
return '';
}
getServerPort() {
return 80;
}
/**
* abstract method for handling express request
* @param {Express.Request} req
* @param {Express.Response} res
*/
handleRequest(req, res) {
}
}
module.exports = BaseService;