-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (66 loc) · 2.41 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
const CLUSTER = require('cluster');
if (CLUSTER.isMaster) {
const NUM_WORKERS = require('os').cpus().length;
//const NUM_WORKERS = 1;
console.log('Master cluster starting up with ' + NUM_WORKERS + ' workers...');
CLUSTER.on('online', worker => {
console.log('Worker ' + worker.process.pid + ' is online');
});
CLUSTER.on('exit', (worker, code, signal) => {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
CLUSTER.fork();
});
for (var i = 0; i < NUM_WORKERS; i++) {
CLUSTER.fork();
}
} else {
const express = require('express');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const errorhandler = require('errorhandler');
const ejs = require('ejs');
const morgan = require('morgan');
const fs = require('fs');
const HTTP = require('http');
global.HTTP = HTTP;
const HTTPS = require('https');
global.HTTPS = HTTPS;
const path = require('path');
const db = require('./service/database');
global.db = db;
const xmljs = require('libxmljs');
global.xmljs = xmljs;
const cheerio = require('cheerio');
global.cheerio = cheerio;
const routes = require('./routes');
const about = require('./routes/about');
const fib = require('./routes/fib');
const APP = express();
// all environments
APP.set('port', process.env.PORT || 8888);
APP.engine('.html', ejs.__express);
APP.set('view engine', 'html');
APP.set('views', __dirname + '/views');
APP.use(favicon('./favicon.ico'));
APP.use(bodyParser.urlencoded({ extended: false }));
APP.use(bodyParser.json());
APP.use(methodOverride());
APP.use(express.static(path.join(__dirname, 'public')));
//APP.use(express.logger('dev'));
const ACCESS_LOG_STREAM = fs.createWriteStream(path.join(__dirname, 'access.log'), {flags: 'a'});
APP.use(morgan('combined', {stream: ACCESS_LOG_STREAM}));
// development only
if ('development' === APP.get('env')) {
APP.use(errorhandler())
}
APP.get('/', routes.index);
APP.get('/rest/fib', fib.get);
APP.get('/about', about.get);
//console.log(CLUSTER.worker.process.pid);
HTTP.createServer(APP).listen(APP.get('port'), function(){
console.log('Express server Worker '+CLUSTER.worker.process.pid+' listening on port ' + APP.get('port'));
});
}