-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
109 lines (91 loc) · 3.29 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
// Require dependencies
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const session = require('express-session');
//Mongo/Mongoose --------------------------------------------------------------
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
const DBconnect = 'mongodb://tiger-foodie:[email protected]:19578/heroku_hlgv59g4';
// Configure DB
mongoose.Promise = Promise;
mongoose.connect(DBconnect, {
useMongoClient: true
});
const db = mongoose.connection;
db.on('error', (err) => {
console.error(`Mongoose error: ${err}`);
});
db.once('openUri', () => {
console.log(`Mongoose connected`);
});
//-------------------------------------------------------------------------------
// Initialize express app
const app = express();
const server = require('http').createServer(app);
var io = require('socket.io')(server);
const PORT = process.env.PORT || 8080;
// Use body parser to parse incoming requests as json
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.text());
app.use(bodyParser.json({
type: 'application/vnd.api+json'
}));
app.use(cookieParser());
// Serve files from the public folder
app.use(express.static(path.resolve(__dirname, 'build')));
//Sets up express routes
const authRoutes = require('./server/routes/auth');
const apiRoutes = require('./server/routes/api');
const secureRoutes = require('./server/routes/secure'); //future secure route
// Pass the authenticaion checker middleware
const authCheckMiddleware = require('./server/middleware/auth-check');
// app.use('/api', authCheckMiddleware);
app.use('/auth', authRoutes);
app.use('/api', apiRoutes);
app.use('/secure', authCheckMiddleware); //future secure route
app.use('/secure', secureRoutes); //future secure route
// Passport ------------------------------------------------------------------
app.use(passport.initialize());
// Load passport strategies
const localSignupStrategy = require('./server/passport/local-signup');
const localLoginStrategy = require('./server/passport/local-login');
passport.use('local-signup', localSignupStrategy);
passport.use('local-login', localLoginStrategy);
//-------------------------------------------------------------------------------
// Serve home page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
//Sets up express to handle 404 NOT FOUND
app.use((req, res) => {
res.status(404).send('404: Sorry the page you requested is not on this server.');
});
//Sets up express to handle 500 INTERNAL SERVER ERROR
app.use((error, req, res) => {
res.status(500).send('500: Internal Server Error');
});
// Start server
server.listen(PORT, () => {
console.log(`The server is listening on port ${PORT}`);
});
//Socket IO
io.on('connection', function(socket) {
socket.on('users', function(data) {
if (data.message == "Store Updated") {
io.emit(data.storeID, {
message: data.message
})
}
if (data.message == "Orders Updated") {
io.emit(data.customerID, {
message: data.message
})
}
});
});