-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
99 lines (88 loc) · 2.38 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
99
/* eslint-disable no-unused-vars */
const express = require('express');
const app = express();
const passport = require('passport');
const crypto = require('crypto');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const cors = require('cors');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const swaggerUI = require('swagger-ui-express');
const swaggerJsDoc = require('swagger-jsdoc');
const path = require('path');
const PORT = process.env.PORT || 5000;
//Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(helmet());
app.use(cookieParser());
// Passport Authentication
require('./server/Config/passport')(passport);
app.use(passport.initialize());
// Import routes
const contestsRoute = require('./server/Routes/contests');
const userRoute = require('./server/Routes/users');
const dogsRoute = require('./server/Routes/dogs');
const resultsRoute = require('./server/Routes/results');
//Connect to DB
dotenv.config();
try {
mongoose.connect(process.env.DB_CONNECT, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (error) {
console.error(error);
}
//Route Middleware
app.use('/api/users', userRoute);
app.use('/api/contests', contestsRoute);
app.use('/api/dogs', dogsRoute);
app.use('/api/results', resultsRoute);
//Swagger
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'Good Boi API',
version: '1.0.0',
description: 'Good Boi API',
},
servers: [
{
url: `http://localhost:${PORT}`,
},
{
url: `https://good-boi-application-lime.vercel.app`,
},
],
},
apis: ['./server/Routes/*.js', './server/Swagger/*.yaml'],
};
app.use(
'/api-docs',
swaggerUI.serve,
swaggerUI.setup(swaggerJsDoc(swaggerOptions)),
);
//Test route
app.get('/api/test', (req, res) => {
res.send('test');
});
//Inject ReactApp into
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client', 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html')),
function (err) {
if (err) {
res.status(500).send(err);
}
};
});
}
//This text will console.log after every save of index.js
app.listen(PORT, () =>
console.log(`The server is running on the port ${PORT}`),
);