-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
66 lines (54 loc) · 1.89 KB
/
index.ts
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
import express from "express";
import session from 'express-session';
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import vmRoutes from "./routes/vmRoutes";
dotenv.config(); // Load environment variables from .env file
const app = express();
app.use(cors());
app.use(express.json());
// Middleware to set CORS headers
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Allow-Origin', '*'); // Allow all origins for testing purposes
next();
});
// Form the MongoDB connection string using environment variables
const { DATABASE_IP, DATABASE_PORT, DATABASE_NAME } = process.env;
const DATABASE_URL = `mongodb://${DATABASE_IP}:${DATABASE_PORT}/vinstadb`;
// Connect to MongoDB
const connectDB = async () => {
try {
await mongoose.connect(DATABASE_URL);
console.log('MongoDB connected successfully');
} catch (error: any) {
console.error('MongoDB connection failed:', error.message);
process.exit(1); // Exit process with failure
}
};
// connectDB();
app.use(express.static(__dirname + '/public'));
app.use("/api", vmRoutes);
const PORT = Number(process.env.PORT || 3333);
const HOST = process.env.HOST || '0.0.0.0';
app.listen(PORT, HOST, () => {
console.log(`Server is running on http://${HOST}:${PORT}`);
});
// Setup express-session middleware
app.use(session({
secret: '123456789', // Testing secret only
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // Set to true if using HTTPS
maxAge: 24 * 60 * 60 * 1000, // 24 hours (session expiration time)
},
}));
process.on("unhandledRejection", (error) => {
console.error("Unhandled Rejection:", error);
});
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
});