forked from LoPaul/Briteboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (44 loc) · 1.38 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const mongoose = require('mongoose');
const socketIo = require('socket.io');
const io = socketIo.listen(server);
io.set('heartbeat timeout', 4000);
io.set('heartbeat interval', 2000);
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
const methodOverride = require('method-override');
app.use(methodOverride('_method'));
// Set view engine
app.set('view engine', 'ejs');
// DB Config
const DB_URI = require('./db/config/keys').mongoURI;
const PORT = process.env.PORT || 3000;
const routes = require("./routes/routes.js")();
// Connect to MongoDB
mongoose.connect(DB_URI, { useNewUrlParser: true })
.then((db) => {
console.log('MongoDB connected!');
// add directory with our static files
app.use(express.static(__dirname + '/public'));
console.log("Server running on 127.0.0.1:3000");
app.use("/boards", routes);
// Temp 404 handler
app.use((req, res) => {
res.status(404).redirect("/");
});
const boards = require('./db/boards');
boards.init(() => {
console.log("In memory cache re-hydrated");
// start web server
server.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
require('./socket-events')(io, boards);
});
})
.catch(err => {
throw(err);
});