-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (47 loc) · 1.37 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
import express from 'express';
import helmet from 'helmet';
import hpp from 'hpp';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import morgan from 'morgan';
import mongoose from 'mongoose';
import path from 'path';
import authRoutes from './routes/api/auth';
import mylistRoutes from './routes/api/mylist';
import config from './config';
const app = express();
const { PORT, MONGO_URI } = config;
const prod = process.env.NODE_ENV === 'production';
app.use(cors({ origin: true, credentials: true }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
if (prod) {
app.use(morgan('combined'));
app.use(hpp());
app.use(helmet({ contentSecurityPolicy: false }));
} else {
app.use(morgan('dev'));
}
mongoose
.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then(() => console.log('MongoDB connection Success'))
.catch((e) => console.log(e));
app.use('/api/auth', authRoutes);
app.use('/api/mylist', mylistRoutes);
if (prod) {
app.use(express.static(path.join(__dirname, './front/build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './front/build', 'index.html'));
});
}
const port = PORT || 4000;
app.listen(port, () => {
console.log('Listening to port: %d', port);
});
export default app;