-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
110 lines (92 loc) · 2.91 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
// 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');
const repoRoutes = require('./routes/repoRoutes');
const morphRoutes = require('./routes/morphRoutes');
const fileRoutes = require('./routes/fileRoutes'); // Added new route import
const { isAuthenticated } = require('./routes/middleware/authMiddleware');
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
app.use(profileRoutes);
// Repo Routes
app.use(repoRoutes);
// Morph Routes
app.use('/', morphRoutes);
// New File Routes
app.use('/', fileRoutes); // Added new route usage
// Root path response
app.get("/", isAuthenticated, (req, res) => {
res.render("index");
});
// 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}`);
});