-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
83 lines (69 loc) · 1.85 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
const express = require('express');
const exphbs = require('express-handlebars');
const app = express();
const mongoose = require('mongoose');
const session = require('express-session');
// database
const database = (module.exports = () => {
const connectionParameters = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
try {
mongoose.connect(
'mongodb+srv://PRJ:[email protected]/?retryWrites=true&w=majority'
),
connectionParameters;
console.log('Database connection successful');
} catch (error) {
console.log(error);
}
});
database();
//Static Files (CSS, Images)
app.use('/static', express.static(`${__dirname}/static`));
// scripts (JS)
app.use('/scripts', express.static(`${__dirname}/scripts`));
// resource files (JSON)
app.use('/res', express.static(`${__dirname}/res`));
// register sessions
app.use(
session({
secret: 'prj2022exploit',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 30000000 },
})
);
// data parsing middleware
// Parse application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
var Handlebars = require('handlebars');
Handlebars.registerHelper('inc', function (value) {
return parseInt(value) + 1;
});
// Handlebars middleware
app.set('view engine', '.hbs');
app.engine(
'.hbs',
exphbs({
extname: '.hbs',
defaultLayout: 'main',
})
);
// routes
app.use('/', require('./routes/main'));
app.use('/users', require('./routes/users'));
app.use('/game', require('./routes/game'));
//err handling
app.use((req, res) => {
res.status(404).send('<i>something broke :/</i>');
});
//port
const HTTP_PORT = process.env.PORT || 8080;
app.listen(HTTP_PORT, (err) => {
if (err) console.log(err);
else console.log(`=> Started at http://localhost:${HTTP_PORT}`);
});
module.exports = session;