-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
61 lines (51 loc) · 1.9 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
////////////////////
// Dependencies //
////////////////////
require("dotenv").config() // make env variables available
const express = require("express")
// const path = require("path") // import path module
const CharacterRouter = require('./controllers/character')
const UserRouter = require('./controllers/user')
const CommentRouter = require('./controllers/comment')
const middleware = require('./utils/middleware')
// SEE MORE DEPENDENCIES IN ./utils/middleware.js
// user and resource routes linked in ./utils/middleware.js
//////////////////////////////
// Middleware + App Object //
//////////////////////////////
const app = require("liquid-express-views")(express())
middleware(app)
/////////////////////////////////////////////
// Home Route
/////////////////////////////////////////////
app.get("/", (req, res) => {
// res.send("Your server is running, better go out and catch it")
// you can also send html as a string from res.send
// res.send("<small style='color: red'>Your server is running, better go out and catch it</small>")
res.render('index.liquid')
})
////////////////////
// Routes //
////////////////////
app.use('/auth', UserRouter)
app.use('/characters', CharacterRouter)
app.use('/comments', CommentRouter)
// app.get('/auth', (req, res) => {
// const { username, userId, loggedIn } = req.session
// res.render('index.liquid', { loggedIn, username, userId })
// })
app.get('/auth', (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 //
//////////////////////////////
app.listen(process.env.PORT, () => {
console.log(`listening on port ${process.env.PORT}`)
})