generated from mieweb/mie-dev-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (61 loc) · 1.97 KB
/
app.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
// app.js
// Main entry point for application
const express = require('express');
const mysql = require('mysql');
const path = require('path');
const bodyParser= require('body-parser');
const app = express();
const { getHomePage} = require('./routes/index');
const game = require('./routes/game');
const game_session = require('./routes/game_session');
const config = require('./config');
const port = config.app.port;
const db = mysql.createConnection(config.db);
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
const createTables = async (db) => {
try {
db.query(`
CREATE TABLE IF NOT EXISTS Boardgame (
boardgame_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
image VARCHAR(255) NOT NULL
)
`);
db.query(`
CREATE TABLE IF NOT EXISTS Session (
session_id INT PRIMARY KEY AUTO_INCREMENT,
boardgame_id INT,
session_date DATE,
session_time TIME,
FOREIGN KEY (boardgame_id) REFERENCES Boardgame(boardgame_id)
)
`);
console.log('Tables created successfully');
} catch (err) {
console.error('Error creating tables', err);
}
};
createTables(db);
global.db = db;
app.set('port', process.env.port || port);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: false }));
// If there are static files, make a public directory
app.use(express.static(path.join(__dirname, 'public')));
// Routes
app.get('/', getHomePage);
app.get('/add-game', game.getAdd);
app.post('/add-game', game.postAdd);
app.get('/edit-game/:id', game.getEdit);
app.post('/edit-game/:id', game.postEdit);
app.get('/add-game-session/:id', game_session.getAdd);
app.post('/add-game-session/:id', game_session.postAdd);
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});