-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (35 loc) · 1.3 KB
/
app.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
require("dotenv").config();
const path = require('path');
const express = require("express");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const Blog = require('./models/blog');
const userRoutes = require("./routes/user");
const blogRoutes = require("./routes/blog");
const { checkForAuthenticationCookie } = require("./middlewares/authentication");
console.log("My name is:", process.env.myname);
const app = express();
const PORT = process.env.PORT || 8000;
// Connecting to MongoDB
mongoose.connect(process.env.MONGO_URL).then(() => console.log("MongoDB connected")); // Local-host URL: "mongodb://localhost:27017/blogify"
// Middleware
app.use(express.urlencoded({extended: false}));
app.use(express.json());
app.use(cookieParser());
app.use(checkForAuthenticationCookie('token'));
app.use(express.static(path.resolve("./public")));
// View-engine
app.set("view engine", "ejs");
app.set('views', path.resolve('./views'));
// Routes
app.get("/", async (req, res) => {
const allBlog = await Blog.find({}).sort({'ctreatedAt': -1});
res.render("home", {
user: req.user,
blogs: allBlog
});
});
app.use("/user", userRoutes);
app.use("/blog", blogRoutes);
// Starting the server
app.listen(PORT, () => console.log(`Server running on PORT: ${PORT}`));