-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
127 lines (106 loc) · 3.93 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Load environment variables
require("dotenv").config();
const mongoose = require("mongoose");
const express = require("express");
const session = require("express-session");
const MongoStore = require('connect-mongo');
const authRoutes = require("./routes/authRoutes");
const profileRoutes = require('./routes/profileRoutes'); // Added profileRoutes
const websiteRoutes = require('./routes/websiteRoutes'); // Added websiteRoutes
const apiRoutes = require('./routes/apiRoutes'); // Added for handling API routes
const pageRoutes = require('./routes/pageRoutes'); // Added for handling Page routes
const sectionRoutes = require('./routes/sectionRoutes'); // Added for handling Section reordering
const headerFooterRoutes = require('./routes/headerFooterRoutes'); // Added for handling Header/Footer editing
const exportRoutes = require('./routes/exportRoutes'); // Added for handling Export functionality
const User = require('./models/User'); // Add this near the top of the file, after other requires
if (!process.env.DATABASE_URL || !process.env.SESSION_SECRET) {
console.error("Error: config environment variables not set. Please create/edit .env configuration file.");
process.exit(-1);
}
const app = express();
const port = process.env.PORT || 3000;
// Middleware to parse request bodies
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Setting the templating engine to EJS
app.set("view engine", "ejs");
// Serve static files
app.use(express.static("public"));
// Database connection
mongoose
.connect(process.env.DATABASE_URL)
.then(() => {
console.log("Database connected successfully");
})
.catch((err) => {
console.error(`Database connection error: ${err.message}`);
console.error(err.stack);
process.exit(1);
});
// Session configuration with connect-mongo
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DATABASE_URL }),
}),
);
app.on("error", (error) => {
console.error(`Server error: ${error.message}`);
console.error(error.stack);
});
// Logging session creation and destruction
app.use((req, res, next) => {
const sess = req.session;
// Make session available to all views
res.locals.session = sess;
if (!sess.views) {
sess.views = 1;
console.log("Session created at: ", new Date().toISOString());
} else {
sess.views++;
console.log(
`Session accessed again at: ${new Date().toISOString()}, Views: ${sess.views}, User ID: ${sess.userId || '(unauthenticated)'}`,
);
}
next();
});
// Authentication Routes
app.use(authRoutes);
// Profile Routes - Added for handling profile page and OpenAI API key updates
app.use(profileRoutes);
// Website Routes - Added for handling 'My Sites' page and website operations
app.use(websiteRoutes);
// API Routes - Added for handling API requests
app.use('/api', apiRoutes);
// Page Routes - Added for handling Page operations
app.use(pageRoutes);
// Section Routes - Added for handling Section reordering
app.use('/api', sectionRoutes);
// Header/Footer Routes - Added for handling Header/Footer editing
app.use(headerFooterRoutes);
// Export Routes - Added for handling Export functionality
app.use(exportRoutes);
// Replace the existing root path response with this:
app.get("/", async (req, res) => {
let apiKeySet = false;
if (req.session.userId) {
const user = await User.findById(req.session.userId);
apiKeySet = user && user.openaiApiKey;
}
res.render("index", { apiKeySet });
});
// If no routes handled the request, it's a 404
app.use((req, res, next) => {
res.status(404).send("Page not found.");
});
// Error handling
app.use((err, req, res, next) => {
console.error(`Unhandled application error: ${err.message}`);
console.error(err.stack);
res.status(500).send("There was an error serving your request.");
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});