-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (67 loc) · 2.8 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
// index.js
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const session = require('express-session');
require('dotenv').config();
const userAuthRoutes = require('./src/routes/authUser.js');
const userRoutes = require('./src/routes/userRoutes.js');
const adminAuthRoutes = require('./src/routes/adminAuth.js');
const adminRoutes = require('./src/routes/adminRoutes.js');
const tenantRoutes = require('./src/routes/tenantRoutes.js');
const verifyTokenRouter = require('./src/routes/authUser.js')
const propertyRoutes = require('./src/routes/propertyRoutes.js');
const adminTenantsRoutes = require('./src/routes/adminTenantsRoutes');
const adminPropertiesRoutes = require('./src/routes/adminPropertiesRoutes');
const invoiceRoutes = require('./src/routes/invoiceRoutes');
const passport = require('./src/middleware/passport-google-auth');
const generateInvoicesMiddleware = require('./src/middleware/invoiceMiddleware');
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
const main = async() => {
const PORT = process.env.PORT;
const mongoURI = process.env.DATABASE_URI;
try{
// Connect to MongoDB
await mongoose.connect(mongoURI)
.then(() => {
console.log('Connected to the Database');
})
.catch(err => console.error('Error connecting to MongoDB:', err));
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
// Define schema and models, perform CRUD operations, etc.
}catch(err) {}
}
main();
// Configure express-session
app.use(session({
secret: 'keyboard cat', // This should be a long, random string used to sign the session ID cookie
resave: false,
saveUninitialized: false
}));
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: 'Internal server error' });
});
app.use(passport.initialize());
app.use(passport.session());
// Use the imported routes
app.use('/api/auth/', userAuthRoutes);
app.use('/api/users', userRoutes);
app.use('/api/tenants', tenantRoutes);
app.use('/api/properties', propertyRoutes);
app.use('/api/invoices', invoiceRoutes);
app.use('/api/auth', verifyTokenRouter)
app.use('/api/auth/admin', adminAuthRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/admin/tenants', adminTenantsRoutes);
app.use('/api/admin/properties', adminPropertiesRoutes);
// Use middleware to generate invoices before processing requests
app.use(generateInvoicesMiddleware);
// const crypto = require('crypto');
// Generate a random secret key
// const secretKey = crypto.randomBytes(32).toString('hex');
// console.log(secretKey);
// Other routes...