-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 2.23 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const session = require('express-session');
const MongoStore = require('connect-mongo');
const registerRoutes = require('./routes/registerRouter');
const loginRoutes=require('./routes/loginRouter');
const logoutRoutes=require('./routes/logoutRouter');
const userRoutes=require('./routes/userRouter');
const passport = require('./config/passport'); // Adjust the path as necessary
const houseRoutes=require('./routes/houseRouter');
const roomRoutes=require('./routes/roomRouter');
const deviceRoutes=require('./routes/deviceRouter');
const copyUserRoutes=require('./routes/copyUserRouter');
const historyRoutes = require('./routes/history'); // Import the history routes
const app = express();
const port = process.env.PORT;
// Configure CORS to allow requests from your React app
app.use(cors({
origin: 'http://localhost:3000', // Replace with your React app's URL
credentials: true
}));
// Middleware to parse JSON
app.use(express.json());
// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
});
// Session management with connect-mongo
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // Set to true if using HTTPS,
maxAge: 30 * 24 * 60 * 60 * 1000
}, // 30 days
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI,
collectionName: 'sessions'
})
}));
// Passport initialization
app.use(passport.initialize());
app.use(passport.session());
// Use the user routes
app.use('/api', registerRoutes);
app.use('/api',loginRoutes);
app.use('/api',logoutRoutes);
app.use('/api',userRoutes);
app.use('/api',houseRoutes);
app.use('/api',roomRoutes);
app.use('/api',deviceRoutes);
app.use('/api',copyUserRoutes);
// Include the history routes
app.use('/api/history', historyRoutes);
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});