-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (69 loc) · 1.89 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
75
76
77
78
79
80
81
import express from 'express';
import morgan from 'morgan';
import compression from 'compression';
import bodyParser from 'body-parser';
import helmet from 'helmet';
import expressGa from 'express-ga-middleware';
import cors from 'cors';
import apiRoutes from './routes/api';
import dbConnection from './config/database';
const app = express();
require('dotenv').config();
// Server configuration
app.use(helmet());
// CORS Managing
app.use(cors());
// Google analytics middleware
app.use(expressGa('UA-127831712-2'));
// Compress the coming requests
function shouldCompress(req) {
if (req.headers['x-no-compression']) {
// don't compress responses with this request header
return false;
}
return true;
}
app.use(compression({ filter: shouldCompress }));
// Middleware for logging http requests
const loggerFormat =
process.env.NODE_ENV === 'development' ? 'dev' : 'combined';
app.use(morgan(loggerFormat));
// Parsing requests
app.use('/uploads', express.static('uploads'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Server Routes
app.get('/', (req, res) => {
res.redirect('/api');
});
app.use('/api', apiRoutes);
// 404 Catching
app.use((req, res, next) => {
const err = new Error('Page not found');
err.status = 404;
return next(err);
});
// Error handler
app.use((err, req, res) => {
res.status(err.status || 500);
res.json({
error: err.message,
});
});
// Run server
const port = process.env.PORT || 3000;
dbConnection
.then(() => {
console.log('CONNECTED TO MONGODB WITH SUCCESS!');
app.listen(port, (error) => {
if (error) process.exit(error);
// eslint-disable-next-line no-console
console.log(`
---
App listening on 'http://localhost:${port}'...
Running on ${process.env.NODE_ENV}
---
`);
});
})
.catch((err) => `AN ERROR HAPPENED ON CONNECTION: ${console.error(err)}`);