forked from mbcse/Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (41 loc) · 1.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
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');
const logger = require('./src/logger');
const connectDB = require('./src/config/db');
const indexRouter = require('./src/routes');
// Start cron job
require('./src/jobs/index');
// Connect Database
connectDB();
const app = express();
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// eslint-disable-next-line no-undef
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404, 'Page not found.'));
});
// error handler
// eslint-disable-next-line no-unused-vars
app.use((error, req, res, _next) => {
if (error instanceof createError.HttpError) {
const obj = {
message: error.message,
};
if (error.errors) {
obj.errors = error.errors;
}
res.status(error.status).json(obj);
} else {
logger.error(error);
res.status(500).json({ message: 'Server error.' });
}
});
module.exports = app;