-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
59 lines (52 loc) · 1.4 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
const express = require("express");
const app = express();
const { create } = require("express-handlebars");
const session = require("express-session");
const varMiddleware = require("./middleware/var");
const cors = require("cors");
const flash = require("connect-flash");
const cookieParser = require("cookie-parser");
const blogRoutes = require("./routes/blog");
const authRoutes = require("./routes/auth");
const port = process.env.PORT || 3000;
require("dotenv").config();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors({ origin: "*", methods: ["GET"] }));
app.use(cookieParser());
app.use(session({ secret: "SECRET_SESSION", saveUninitialized: false }));
app.use(flash());
app.use(varMiddleware);
app.use(blogRoutes);
app.use(authRoutes);
// hbs config
const hbs = create({
defaultLayout: "main",
extname: "hbs",
});
// handlebars settings
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
app.set("views", "./views");
// Routes
app.get("/", async (req, res) => {
res.render("index", {
title: "Home",
});
});
// connect mongoose
const mongoose = require("mongoose");
mongoose
.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("connected to mongoose");
})
.catch((err) => {
console.log(err);
});
app.listen(port, () => {
console.log("listening on port: " + port);
});