-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
57 lines (49 loc) · 1.39 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
(() => {
'use strict';
const ENV = process.env.NODE_ENV || 'development';
let express = require('express'),
cluster = require('cluster'),
moment = require('moment'),
app = express(),
bodyParser = require('body-parser');
module.exports = (appdir, cb) => {
app.dir = appdir;
// static files
app.use(express.static(app.dir + '/public'));
// things to do on each request
app.use((req, res, next) => {
// log each request in development/staging ENVironment
if (ENV !== 'production') {
console.log(moment().format('HH:MM'), req.method, req.url,
req.socket.bytesRead, 'process:', process.pid);
}
next();
});
// Standard error handling
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
next();
});
// to support JSON-encoded bodies
app.use(bodyParser.json());
// to support URL-encoded bodies
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/cluster', (req, res) =>
res.send({
isMaster: cluster.isMaster,
isWorker: cluster.isWorker,
workers: cluster.workers,
pid: process.pid
})
);
// Dummy route to return JSON
app.get('/', (req, res) => {
res.send(require('http').STATUS_CODES);
});
// callback from /index.js
cb(app);
};
})();