-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (44 loc) · 1.6 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
//required dependencies
require("dotenv").config();
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const db = mongoose.connection;
const methodOverride = require("method-override");
const session = require("express-session");
//controllers
const characterController = require("./controllers/characters.js");
const userController = require("./controllers/users.js");
const sessionsController = require("./controllers/sessions.js");
//env variables
const mongoURI = process.env.MONGOURI || "mongodb://127.0.0.1:27017/";
const port = process.env.PORT || 3000;
//MIDDLEWARE
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(methodOverride("_method"));
app.use(
session({
secret: process.env.SECRET, //a random string do not copy this value or your stuff will get hacked
resave: false, // default more info: https://www.npmjs.com/package/express-session#resave
saveUninitialized: false, // default more info: https://www.npmjs.com/package/express-session#resave
})
);
//controller middleware
app.use("/characters", characterController);
app.use("/users", userController);
app.use("/sessions", sessionsController);
app.listen(port, () => console.log("listening on port ", port));
mongoose.connect(mongoURI + "char-manager", (err) => {
if(err) {
console.log("ERROR: ", err)
} else {
console.log("The connection with mongo is established");
}
});
app.get("/", (req, res) => {
res.render("home.ejs", {
currentUser: req.session.currentUser,
});
});