-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (80 loc) · 3.05 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
90
91
92
93
94
95
96
97
98
99
100
101
// ///////////////////////////////////////////////////////
// <--Server.js--> //
// This file is where our server is created. //
// The LionBot app starts here, //
// importing all necessary pkgs./files to run the app //
// //
// //////////////////////////////////////////////////////
// express package: will initialize the usage of express, our back-end framework
const express = require('express')
// morgan package: middleware used to log a console message everytime a request is made
const logger = require('morgan')
// path
const path = require('path')
// body-parser: allows us to parse data the way we want (JSON, plain text) before using it
// neccessary to use req.body
const bodyParser = require('body-parser')
// packages needed to establish user authentication for app
// cookie-parser will allow us access to user cookies during sessions
// session will allow us to store data during each user session
// passport is the main package handling auth
const cookieParser = require('cookie-parser')
const session = require('express-session')
const passport = require('passport')
// creating a new instance of express
const app = express()
// .env file will store our SECRET_KEY and keep it hidden
require('dotenv').config()
// allwoing morgn to log data from every request
app.use(logger('dev'))
// parses data into json for every request
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
// allow us to use cookie-parser on every request
app.use(cookieParser())
// basic configuration settings for sessions
app.use(
session({
key: process.env.SECRET_KEY,
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: true
})
)
// initializes passport(auth) checks on user sessions
app.use(passport.initialize())
app.use(passport.session())
// allows us acess to static files in our public folder
app.use(express.static('public'))
// port number this server will be accessible from
const PORT = process.env.PORT || 3001
// logs a messsage every time the server is started
app.listen(PORT, () => {
console.log(`🤖 Listening on port ${PORT}`)
})
// sends this message when rooth path is accessed
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
// imports auth-routes file to use for '/api/auth' path requests
const authRoutes = require('./routes/auth-routes')
app.use('/api/auth', authRoutes)
// imports command routes file to use for '/api/commands' path requests
const commandRoutes = require('./routes/command-routes')
app.use('/api/commands', commandRoutes)
// handles error messages to paths that are not detailed here, request errors
app.use('*', (req, res) => {
res.status(400).json({
message: 'Not Found!'
})
})
// handles error messages that deal with server calls not being recogzied
app.use((err, req, res, next) => {
console.log(err)
res.status(500).json({
error: err,
message: err.message
})
})
// export app for testing
module.exports = app