-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
89 lines (70 loc) · 2.38 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
////////////////////
// Dependencies //
////////////////////
require("dotenv").config() // make env variables available
const express = require("express")
const mongoose = require("mongoose")
const path = require("path")
const SkincareRouter = require('./controllers/skincare.js')
const UserRouter = require('./controllers/user.js')
const CommentRouter = require('./controllers/comment.js')
const middleware = require('./utils/middleware')
const User = require("./models/user")
// SEE MORE DEPENDENCIES IN ./utils/middleware.js
// user and resource routes linked in ./utils/middleware.js
/////////////////////////////////////////////
// Database Connection
/////////////////////////////////////////////
// Setup inputs for our connect function
const DATABASE_URL = process.env.DATABASE_URL
const CONFIG = {
useNewUrlParser: true,
useUnifiedTopology: true,
}
// Establish Connection
mongoose.connect(DATABASE_URL, CONFIG)
// Events for when connection opens/disconnects/errors
mongoose.connection
.on("open", () => console.log("Connected to Mongoose"))
.on("close", () => console.log("Disconnected from Mongoose"))
.on("error", (error) => console.log(error))
//////////////////////////////
// Middleware + App Object //
//////////////////////////////
const app = require("liquid-express-views")(express())
middleware(app)
////home route/////
app.get("/", (req, res) => {
if (req.session.loggedIn) {
res.redirect('/skincares')
} else {
res.render('/index.liquid')
}
})
////////////////////
// Routes //
////////////////////
app.use('/auth', UserRouter)
app.use('/comments', CommentRouter)
app.use('/skincares', SkincareRouter)
////renders error page, gets the error from url request query
app.get('/', (req, res) => {
const { username, userId, loggedIn } = req.session
res.render('/index.liquid', { loggedIn, username, userId })
})
app.get('/error', (req, res) => {
const error = req.query.error || 'This Page Does Not Exist'
const { username, loggedIn, userId } = req.session
res.render('/error.liquid', { error, username, loggedIn, userId })
})
// if page is not found, send to error page
app.all('*', (req, res) => {
res.redirect('/error')
})
//////////////////////////////
// App Listener //
//////////////////////////////
const PORT = process.env.PORT
app.listen(process.env.PORT, () => {
console.log(`listening on port ${process.env.PORT}`)
})