-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (78 loc) · 2.24 KB
/
index.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
95
96
97
98
import express from 'express';
import { json } from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import morgan from 'morgan';
import { join } from 'path';
import rfs from 'rotating-file-stream';
import dotenv from 'dotenv';
import logger from './src/logger/logger';
import connectDatabase from './src/configs/db.config';
import YoutubeBackgroundTasks from './src/tasks/video.background';
import YoutubePlayListBackgroundTasks from './src/tasks/playlist.background';
/* istanbul ignore next */
dotenv.config();
// configure isProduction variable
const isProduction = process.env.NODE_ENV === 'production';
const port = process.env.PORT || 3000;
// defining the Express app
const app = express();
// adding Helmet to enhance your API's security
app.use(helmet());
// using bodyParser to parse JSON bodies into JS objects
app.use(json());
// enabling CORS for all requests
app.use(cors());
// create a rotating write stream
const accessLogStream = rfs('access.log', {
interval: '1d', // rotate daily
path: join(__dirname, 'log'),
});
// adding morgan to log HTTP requests
app.use(morgan('dev', { stream: accessLogStream }));
// connect to mongo
connectDatabase();
// auto update youtube videos
YoutubeBackgroundTasks.autoUpdateYoutubeVideos.start();
YoutubePlayListBackgroundTasks.autoUpdateYoutubePlaylist.start();
if (!isProduction) {
// eslint-disable-next-line global-require
app.use(require('errorhandler')());
}
app.get('/', (req, res) => {
logger.info('GET /');
res.send('App works!!!!!');
});
app.use('/api', require('./src/routes/routes').default);
// request to handle undefined or all other routes
app.get('*', (req, res) => {
logger.info('GET undefined routes');
res.send('App works!!!!!');
});
// Error handlers & middlewares
if (!isProduction) {
app.use((err, req, res) => {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: err,
},
});
});
}
app.use((err, req, res) => {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: {},
},
});
});
// starting the server
app.listen(port, () => {
logger.info(`listening on port ${port}`);
});
// Export our app for testing purposes
export default app;