-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
72 lines (65 loc) · 2.25 KB
/
server.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
"use strict";
const express = require('express');
const app = express();
const port = process.env.PORT || 9000;
const formData = require('express-form-data');
const cors = require('cors');
const passport = require('passport');
const cookieSession = require('cookie-session');
const {keys, passportSetup} = require('./config/ExportConfig');
const Mongo = require('mongodb');
const MongoClient = Mongo.MongoClient;
const client = new MongoClient(keys.mongodb.dbURI, { useNewUrlParser: true, useUnifiedTopology: true });
const {authRouter, workoutRouter, nutritionRouter, userRouter, storyRouter} = require('./Routers/ExportRouters');
const corsOptions = {
origin: 'https://abetteruhealth.com',
optionsSuccessStatus: 200,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true
}
app.use(cookieSession({
maxAge: 7 * 24 * 60 * 60 * 1000,
keys: [keys.session.cookieKey]
}));
app.use(formData.parse());
app.use(express.json());
app.use(passport.initialize());
app.use(passport.session());
app.use(cors(corsOptions));
app.enable('trust proxy');
client.connect((err)=> {
console.log('Successfully connected to the server')
const db = client.db('a-better-u');
app.locals.db = db;
app.locals.exerciseList = db.collection('exerciseList');
app.locals.nutrition = db.collection('nutrition');
app.locals.routines = db.collection('routines');
app.locals.stories = db.collection('stories');
app.locals.users = db.collection('users');
app.locals.workouts = db.collection('workouts');
})
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
//Middleware to get today's date
app.use((req, res, next) => {
let options = {month: "2-digit", day: "2-digit", year: "numeric"};
let date = new Date();
let today = date.toLocaleDateString("en-US", options);
app.locals.date = today;
next()
});
const isAuthenticated = (req, res, next) => {
if(req.user){
next();
}
else{
res.status(401).send({error: "Not authenticated!"});
}
};
// Routers for the workout, nutrition, and story pages and user information
app.use('/auth', authRouter);
app.use('/workout', isAuthenticated, workoutRouter);
app.use('/nutrition', isAuthenticated, nutritionRouter);
app.use('/user', isAuthenticated, userRouter);
app.use('/story', isAuthenticated, storyRouter);