forked from kriskuiper/project-tech-team
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (52 loc) · 1.99 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
60
61
// Require dependencies from node_modules needed for the server
const express = require("express");
const session = require("express-session");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
require("dotenv").config();
// Require controllers
const serveHome = require("./app/controllers/serveHome");
const renderCreateAccount = require("./app/controllers/renderCreateAccount");
const createAccount = require("./app/controllers/createAccount");
const login = require("./app/controllers/login");
const logout = require("./app/controllers/logout");
const renderLogin = require("./app/controllers/renderLogin");
const renderFeed = require("./app/controllers/renderFeed");
const renderForm = require("./app/controllers/renderForm");
const addPost = require("./app/controllers/addPost");
const renderPostDetail = require("./app/controllers/renderPostDetail");
const serveNotFound = require("./app/controllers/serveNotFound");
// Process environment vars and connect to database
const uri = process.env.MONGODB_URI;
mongoose.set("useNewUrlParser", true);
mongoose.connect(uri);
// Process secret and define sess for using express-session
const secret = process.env.SESSION_SECRET;
const sess = {
resave: false,
saveUninitialized: true,
secret: secret,
};
const app = express();
const port = 8000;
app
.use("/static", express.static("app/static"))
.use(bodyParser.urlencoded({extended: true}))
.use(session(sess))
.set("view engine", "ejs")
.set("views", "app/view")
.get("/", serveHome)
.get("/create-account", renderCreateAccount)
.get("/log-in", renderLogin)
.get("/log-out", logout)
.get("/my-feed", renderFeed)
.get("/add-post", renderForm)
.get("/my-feed/:url", renderPostDetail)
.post("/", createAccount)
.post("/my-feed", addPost)
.post("/log-in", login)
.use(serveNotFound)
.listen(process.env.PORT || port, listening);
function listening() {
console.log(`App is on port ${port}`); // eslint-disable-line
}