-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (58 loc) · 1.98 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
62
63
64
65
66
// ---------------------------------
// DEPENDENCIES
// ---------------------------------
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const session = require("express-session");
const methodOverride = require("method-override");
// ---------------------------------
// PORT (set up for hosting w/ heroku)
// ---------------------------------
const port = process.env.PORT || 3000;
// ---------------------------------
// DATABASE (set up for hosting w/ heroku w/ mLab add on)
// ---------------------------------
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/app_dev';
// ---------------------------------
// MIDDLEWARE
// ---------------------------------
app.use(express.urlencoded({extended:false}));
app.use(express.json());
app.use(express.static("public"));
app.use(methodOverride("_method"));
app.use(session({
secret: "feedmeseymour",
resave: false,
saveUninitialized: false
}));
// ---------------------------------
// CONTROLLERS
// ---------------------------------
const usersController = require("./controllers/users.js");
app.use("/users", usersController);
const sessionsController = require("./controllers/sessions.js");
app.use("/sessions", sessionsController);
// ---------------------------------
// ROUTES
// ---------------------------------
// localhost:3000 - this will render the main index page/home page
app.get("/", (req, res)=>{
res.render("index.ejs");
});
// 404 errors! - this will catch any route that doesn't exist
app.get("*", (req, res) => {
res.render("four-oh-four/index.ejs");
});
// ---------------------------------
// LISTENER & MONGOOSE CONNECTION
// ---------------------------------
app.listen(port, ()=>{
console.log("-----------------------------");
console.log("I'm listening on port: ", port);
console.log("-----------------------------");
});
mongoose.connect(mongoUri, {useNewUrlParser: true});
mongoose.connection.on("open", () => {
console.log("connected to mongoose!");
});