-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
94 lines (75 loc) · 2.45 KB
/
server.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
92
93
94
/* Express */
let express = require('express')
let app = express()
/* Packages */
//let newrelic = require('newrelic');
let compression = require('compression')
let mongoose = require('mongoose');
let morgan = require('morgan');
let bodyParser = require('body-parser');
let cors = require('cors');
/* Routes */
let classroomPublic = require('./routes/classroomPublic.js');
let classroomPrivate = require('./routes/classroomPrivate.js');
let classroomWebhook = require('./routes/classroomWebhook.js');
let chatPrivate = require('./routes/chatPrivate.js');
/* CLI Arguments */
let parseArgs = require('minimist')
/* Port */
let port;
if (process.env.NODE_ENV == 'dev') {
var args = parseArgs(process.argv.slice(2), { port: 'port' });
if (args.port == undefined) {
throw new Error("To start microservice, must define PORT argument. --port <num>");
}
port = args.port;
} else if (process.env.NODE_ENV == 'test' || process.env.NODE_ENV == 'acceptance') {
port = process.env.PORT || 8080;
} else if (process.env.NODE_ENV == 'prod') {
require('newrelic');
port = 80;
} else {
throw new Error("Set up development variable. 'dev', 'test', 'acceptance', 'prod'");
}
/* End CLI Arguments */
/* Configs */
let config = require('config');
/* Database options */
let options = {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
// useMongoClient: true
};
console.log(config.DBHost);
/* Database */
mongoose.connect(config.DBHost, options);
let db = mongoose.connection;
/* Mongoose fix depreciation promise */
mongoose.Promise = Promise;
/* Database # Error Handling */
db.on('error', console.error.bind(console, 'connection error:'));
/* Testing environment */
if (config.util.getEnv('NODE_ENV') !== 'test') {
//use morgan to log at command line
app.use(morgan('combined')); //'combined' outputs the Apache style LOGs
}
/* app.use # packages */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: 'application/json' }));
app.use(compression());
app.use(cors());
/* app.user custom modification */
/* Accounts routes */
app.use('/classroom', classroomWebhook);
app.use('/classroom', classroomPublic);
app.use('/classroom', classroomPrivate);
app.use('/classroom', chatPrivate);
/* Start API */
app.listen(port, function() {
console.log('# Running on port ' + port + ' #');
})
module.exports = app;