-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (57 loc) · 1.9 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
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
require('dotenv').config(); // Load environment variables
const Blog = require("./models/blog");
const User = require("./models/user");
const userRoute = require('./routes/user');
const blogRoute = require('./routes/blog');
const { checkForAuthenticationCookie } = require("./middlewares/authentication");
const app = express();
const PORT = process.env.PORT || 8000;
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/blogiFy", { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB Connected"))
.catch(err => console.error("MongoDB Connection Error:", err));
// Set up view engine
app.set("view engine", "ejs");
app.set("views", path.resolve("./views"));
// Middleware
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Authentication middleware
app.use(checkForAuthenticationCookie("token"));
// Populate res.locals.user
app.use((req, res, next) => {
if (req.user) {
res.locals.user = {
fullName: req.user.fullName,
profileImageURL: req.user.profileImageURL
};
} else {
res.locals.user = null;
}
next();
});
// Routes
app.get("/", async (req, res) => {
try {
const allBlogs = await Blog.find({});
res.render("home", {
blogs: allBlogs,
});
} catch (err) {
console.error("Error fetching blogs:", err);
res.status(500).send("Server Error");
}
});
app.use('/user', userRoute);
app.use('/blog', blogRoute);
// Start server
app.listen(PORT, () => {
console.log(`Server started at PORT:${PORT}`);
});