forked from Girl-Code-It/Opportunity-Calendar-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (78 loc) · 2.95 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
82
83
84
85
86
87
88
// import { config } from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import express from 'express';
const app = express();
import pkg from 'mongoose';
const { connect } = pkg;
import methodOverride from 'method-override';
import cors from 'cors';
import swaggerUiExpress from 'swagger-ui-express';
import swaggerJSDoc from 'swagger-jsdoc';
const { json } = express;
const { serve, setup } = swaggerUiExpress;
import passport from 'passport';
import passport_init from './authentication/strategies/google.js';
passport_init();
import morgan from 'morgan';
import { db_user, db_pwd, db_host, db_name } from './config.js';
//requiring routes
import indexRoutes from './routes/index.js';
import opportunityRoutes from './routes/opportunity.js';
import authRoutes from './routes/authenticate.js';
//Logging Services
app.use(morgan('common'));
//Set this string for testing purpost if you want to connect to local non-password protected mongodb
//DO NOT FORGET TO COMMENT THIS ONE
// const mongoSrvString = `mongodb://${db_host}/${db_name}?retryWrites=true&w=majority`;
//DO NOT FORGET TO UNCOMMENT THIS ONE
const mongoSrvString = `mongodb+srv://${db_user}:${db_pwd}@${db_host}/${db_name}?retryWrites=true&w=majority`;
// connect the database
connect(mongoSrvString, {
useNewUrlParser: true,
useUnifiedTopology: true, //significant refactor of how it handles monitoring all the servers in a replica set or sharded cluster.
//In MongoDB parlance, this is known as server discovery and monitoring.
useCreateIndex: true,
useFindAndModify: true,
})
.then(() => {
console.log('Connected to mongo db');
})
.catch((err) => {
console.log("Couldn't connect to mongo db, err: ", err);
});
app.use(cors());
// in order to read HTTP POST data , we have to use "body-parser" node module. body-parser is a piece of express middleware that reads a form's input and stores it as a javascript object accessible through req.body
// app.use(bodyParser.urlencoded({ extended: true })); //middleware for parsing bodies from URL.
//app.use(bodyParser.json());
// express has got its own middleware for bodyparsing, use this as an alternative to bodyparser.json()
app.use(json());
app.use(passport.initialize());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.use(methodOverride('_method')); //to support HTTP Verbs other than GET,POST
app.use('/', indexRoutes);
app.use('/opportunity', opportunityRoutes);
app.use('/auth/',authRoutes);
// swagger
app.use('/playground', serve);
app.get(
'/playground',
setup(
swaggerJSDoc({
definition: {
openapi: '3.0.0',
},
apis: ['./routes/*.js'], // files containing annotations as above
})
)
);
const start = () => {
const port = process.env.PORT || 3030;
app.listen(port, function () {
console.log(`[OK] server started on port ${port}`);
});
};
export default start;