-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (55 loc) · 1.64 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
70
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const passport = require("passport");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const methodOverride = require("method-override");
const flash = require("express-flash");
const logger = require("morgan");
const connectDB = require("./config/db");
const adminRoutes = require("./routes/admin");
const photoRoutes = require("./routes/photos");
const cors = require('cors')
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 8000
//Use .env file in config folder
require("dotenv").config({ path: "./config/.env" });
// Passport config
require("./config/passport")(passport);
//Connect To Database
connectDB();
//Using EJS for views
app.set("view engine", "ejs");
//Static Folder
app.use(express.static("public"));
//CORs use
app.use(cors())
//Body Parsing
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Logging
app.use(logger("dev"));
//Use forms for put / delete
app.use(methodOverride("_method"));
// Setup Sessions - stored in MongoDB
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
//Use flash messages for errors, info, ect...
app.use(flash());
//Setup Routes For Which The Server Is Listening
app.use("/", adminRoutes);
app.use("/api", photoRoutes);
//Server Running
app.listen(PORT, () => {
console.log("Server is running on:", PORT);
});