-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (42 loc) · 1.23 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
const express = require('express');
const mongoose = require('mongoose');
const createError = require('http-errors');
const { ROUTE } = require('./configs/constants');
const initLoaders = require('./loaders');
const usersRouter = require('./routes/users');
const app = express();
initLoaders(app);
app.use(ROUTE.USERS, usersRouter);
app.use((req, res, next) => {
next(createError(404));
});
app.use((err, req, res, next) => {
console.log(err);
if (process.env.NODE_ENV === 'production') {
if (err instanceof mongoose.Error) err = createError(500);
err.stack = null;
}
res.status(err.status || 500);
res.json(err);
});
let server;
if (process.env.NODE_ENV === 'development') {
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem'),
passphrase: 'stst',
requestCert: false,
rejectUnauthorized: false,
};
server = https.createServer(options, app).listen(443, function () {
console.log('Https server listening');
});
} else {
server = app.listen(process.env.PORT || 8080, () => {
console.log('server listening');
});
}
require('./configs/socket').init(server);
require('./lib/socketHandler');