-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (62 loc) · 2 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
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const mongoose = require('mongoose');
const passport = require('passport');
const passportConfig = require('./midllewear/passport');
const ScheduledJobs = require('./Jobs/ScheduledJobs');
const ApplicationError = require('./errors/applicationError');
const UnprocessableEntityError = require('./errors/unprocessableEntity');
const indexRouter = require('./routes/index');
const userRouter = require('./routes/user.routes');
const statRouter = require('./routes/stat.routes');
const { MONGO_URI } = require('./config/key.config');
const app = express();
(async () => {
try {
await mongoose.connect(MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
console.log('MongoDB connected');
const Jobs = new ScheduledJobs();
Jobs.runJobs();
} catch (e) {
console.log(e);
}
})();
app.use(passport.initialize());
passportConfig(passport);
app.use(morgan('dev'));
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/', indexRouter);
app.use('/user', userRouter);
app.use('/stat', statRouter);
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
if (err instanceof UnprocessableEntityError) {
console.log('error: ', err);
res.status(err.getStatus()).json({
field: err.getField(),
message: err.message,
});
} else if (err instanceof ApplicationError) {
console.log('error: ', err);
res.status(err.getStatus()).send();
} else {
console.log('error: ', err);
res.status(500).send();
}
});
app.use(function (req, res) {
res.status(404).json({
message: 'error 404',
});
});
module.exports = app;