-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
115 lines (93 loc) · 3.5 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//setup express app
const express = require('express');
const app = express();
var router = express.Router();
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const http = require('http').Server(app);
//socket.io
const io = require('socket.io')(http);
//Whenever someone connects this gets executed
io.on('connection', function (socket) {
console.log('A user connected');
//send event to front end
setTimeout(function () {
// Sending an object when emmiting an event
socket.emit('testerEvent', { description: 'A custom event named testerEvent!' });
}, 4000);
//receive event from front end
socket.on('clientEvent', function (data) {
console.log('clientEvent recieved',data);
});
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
//port and hosting info
const port = 8080;
//connect to mongodb server
var mongodbutil = require('./static/assets/js/mongodbutils');
mongodbutil.connectToServer(function (err) {
//app goes online once this callback occurs
if (err) {
console.log('mongodbutil.connectToServer err=', err)
}
// error handler
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error');
});
//end of calback
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
//Loads the handlebars module
const handlebars = require('express-handlebars');
//Sets our app to use the handlebars engine
app.set('view engine', 'handlebars');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
//Sets handlebars configurations
app.engine('handlebars', handlebars({
layoutsDir: __dirname + '/views/layouts',
helpers: {
'ifEquals': function (arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
},
'ifActiveId': function (arg1, arg2, options) {
console.log('ifActiveId: options=', options)
//return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
return true;
}
},
}));
//Tells app to use '/public' folder for static files
app.use(express.static('public'))
//connect all routes
const routes = require('./routes');
app.use('/', routes);
//use this folder for static files
app.use('/static/', express.static(__dirname + '/static/'));
//render and startup server
http.listen(port, function () {
console.log(`App listening to port ${port}`)
});
});
module.exports = {
app:app,
io:io
};