-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (58 loc) · 2.46 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
const express = require('express') //Express HTTP Server
const session = require('express-session') //Session Management
const cookieParser = require('cookie-parser') //Session cookie reading
const bodyParser = require('body-parser') //HTTP -> JSON
const passport = require("passport") //Auth lib
const cors = require('cors') //TODO: Handle cross-origin requests
const morgan = require('morgan') //Logger
const {Sequelize} = require('sequelize') //ORM for MySQL
require('dotenv').config(); //Initialize environment vars
// ----------------------- END IMPORTS ----------------------- \\
//Initializes express application
const app = express();
const port = process.env.PORT || 5000;
//Initialize MySQL ORM
//TODO switch to env connection vars
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, { // db, u, p
host: 'localhost',
dialect: 'mysql',
logging: false,
define: {
timestamps: false
}
})
//Parse JSON from HTTP
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
//Log HTTP Requests in console
app.use(morgan('dev'))
app.use(
cors()
);
// ----------------------- END MIDDLEWARE ----------------------- \\
const compRoute = require('./routes/competitions')(sequelize); //Competitions reqs routed to routes/auth.js
app.use('/competitions', compRoute)
const pitScoutingRoute = require('./routes/pitscouting')(sequelize); //Pit Scouting reqs routed to routes/auth.js
app.use('/pitscouting', pitScoutingRoute)
const seasonsRoute = require('./routes/seasons')(sequelize); //Pit Scouting reqs routed to routes/auth.js
app.use('/seasons', seasonsRoute)
const teamsRoute = require('./routes/teams')(sequelize); //Pit Scouting reqs routed to routes/auth.js
app.use('/teams', teamsRoute)
const scoutingRoute = require('./routes/scouting')(sequelize); //Pit Scouting reqs routed to routes/auth.js
app.use('/scouting', scoutingRoute)
// ----------------------- END ROUTING ----------------------- \\
app.use((err, req, res, next) => { //Error logging
console.error(err)
next();
})
app.listen(port, async () => { //Starts server & connects to MySQL DB
try {
await sequelize.authenticate();
console.log('MySQL Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
console.log(`Server listening on port ${port}!`)
});