-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
69 lines (59 loc) · 1.85 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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// const session = require('express-session');
// const MongoDBStore = require('connect-mongodb-session')(session);
const cors = require('cors')
const User = require('./models/user');
const errorController = require('./controllers/error');
const MONGODB_URI = 'mongodb+srv://youssefelsawy:7WE62UIa4j25Yd31@cluster0.4uq3vsh.mongodb.net/pocketTrainer?retryWrites=true&w=majority'
const app = express();
// const store = new MongoDBStore({
// uri: MONGODB_URI,
// collection: 'sessions'
// });
const adminRoutes = require('./routes/admin');
const userRoutes = require('./routes/user');
const authRoutes = require('./routes/auth')
const exeRoutes = require('./routes/Exercises')
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Add a middleware to set the Access-Control-Allow-Origin header to '*'
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
// app.use(
// session({
// secret: 'my secret',
// resave: false,
// saveUninitialized: false,
// store: store
// })
// );
app.use(authRoutes);
// app.use((req, res, next) => {
// User.findById(req.session.userId)
// .then(user => {
// req.user = user;
// next();
// })
// .catch(err => {console.log(err)});
// })
app.use('/admin', adminRoutes);
app.use(userRoutes);
app.use(exeRoutes);
app.use((error, req, res, next) => {
console.error(error); // Log the error for debugging purposes
res.status(500).json({ error: 'Internal server error' });
});
app.use(errorController.get404);
mongoose.connect(MONGODB_URI)
.then(result => {
app.listen(8080);
console.log('connected!');
})
.catch(err => {
console.log(err);
})