-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
62 lines (52 loc) · 1.68 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
58
59
60
61
62
'use strict';
const express = require('express');
const config = require('./config/config');
const middleware = require('./middlewares/middleware');
const databaseUtil = require('./utils/database');
const indexerUtil = require('./utils/indexer');
const databaseSetup = require('./utils/databaseSetup');
const logger = require('./logger/debugLogger');
const app = express();
let server = {};
app.use(middleware);
const onServerError = function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
const port = config.port;
const bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
logger.log('error', bind + ' requires elevated privileges', error);
process.exit(1);
break;
case 'EADDRINUSE':
logger.log('error', bind + ' is already in use', error);
process.exit(1);
break;
default:
throw error;
}
};
const start = function () {
return databaseUtil.connectDatabase()
.then(databaseSetup)
.then(indexerUtil.init)
.then(function () {
server = app.listen(config.port, config.host, function () {
console.log('Book sharing application started at address: ' + server.address().address + ' port: ' + server.address().port);
});
return server.on('error', onServerError);
}).catch(function (err) {
logger.log('error', 'Could not start server', err);
})
};
if(config.env != 'test') {
start();
}
module.exports = {
start: start
};