-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
99 lines (73 loc) · 2.15 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
// server.js this file is the initial starting point for the Node/Express server.
require('dotenv').config()
// Dependencies
const express = require(`express`);
const path = require('path');
const bodyParser = require(`body-parser`);
const Pusher = require('pusher');
//=========================================
const pusher = new Pusher({
appId: process.env.APPID,
key: process.env.API_KEY,
secret: process.env.SECRET,
cluster: 'us3',
encrypted: true
});
//====================================================
const app = express();
const PORT = process.env.PORT || 9000;
// Requiring models for syncing
const db = require(`./models`);
// sets up the express app to handle data parsing
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
//static directory
app.use(express.static(path.join((__dirname, `public`))));
// Set Handlebars.
const exphbs = require(`express-handlebars`);
app.engine(`handlebars`, exphbs({
defaultLayout: `main`
}));
app.set(`view engine`, `handlebars`);
//import routes and give the server access to them.
//=======================================================
//posting app route for pusher
app.post('/comment', function (req, res) {
console.log(`comment handler`);
console.log(req.body);
var newComment = {
name: req.body.name,
email: req.body.email,
comment: req.body.comment,
time: req.body.time
}
db.bashingTable.create({
userName: req.body.name,
userEmail: req.body.email,
userComment: req.body.comment,
createdAt: req.body.time
}).then(function (response) {
console.log(response);
pusher.trigger('Basher-staging', 'new_comment', newComment);
res.json({
created: true
});
})
});
//routes
//===============================================
require(`./routes/htmlRoutes`)(app);
// REQUIRING API ROUTES
require(`./routes/apiRoutes`)(app);
//syncing sequelize models and then starting Express app
module.exports = app;
//===========================================================
db.sequelize.sync({
force: true
}).then(function () {
app.listen(PORT, function () {
console.log(`App listening on PORT ` + PORT);
});
});