-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (46 loc) · 1.71 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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const flash = require("connect-flash");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const ObjectId = require("mongodb").ObjectId;
const configDB = require("./config/database.js");
const PORT = 3000;
require("dotenv").config({ path: "./config/.env" });
mongoose.connect(configDB.url, (err, database) => {
if (err) return console.log(err);
console.log(`Connected to ${configDB.dbName} DB 🗃'`);
let db = database;
// routes
require("./app/routes.js")(app, passport, db, ObjectId);
}); // connect to our database
require("./config/passport")(passport); // pass passport for configuration
// set up express application
app.use(morgan("dev")); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.set("view engine", "ejs");
// required for passport
app.use(
session({
secret: "keyboard cat bongo",
resave: true,
saveUninitialized: true,
cookie: { maxAge: 1000 * 60 * 60 * 24 }, // Cookie Life - 24 hours
})
); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// launch 🚀
app.listen(process.env.PORT || PORT, function () {
console.log(
`🚨 The server is listening on port ${PORT} you better go catch it!!! 🚨`
);
});