-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (33 loc) · 1.11 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
require('dotenv').config()
const express = require('express')
const path = require('path')
const mongoose = require("mongoose")
const cookieParser = require('cookie-parser');
const Blog = require('./models/blog');
const userRoute = require('./routes/user');
const blogRoute = require('./routes/blog');
const { checkForAuthenticationCookie } = require('./middlewares/authentication');
const app = express();
const PORT = process.env.PORT || 3000;
// Connect MongoDB
mongoose
.connect(process.env.MONGO_URL)
.then((e) => console.log("MongoDB connected"))
// Middlewares
app.set('view engine', "ejs")
app.set("views", path.resolve('./views'))
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser());
app.use(checkForAuthenticationCookie('token'));
app.use(express.static(path.resolve('./public')));
// Routes
app.get('/', async (req, res) => {
const allBlogs = await Blog.find({});
res.render('home', {
user: req.user,
blogs: allBlogs,
})
});
app.use('/user', userRoute);
app.use('/blog', blogRoute);
app.listen(PORT, () => console.log(`Server Started at PORT ${PORT}`));