-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (71 loc) · 2.44 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
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
// define folder to serve assets from
app.use(express.static(path.join(__dirname, "public")));
// ----------------------------- HANDLEBARS -----------------------------
const exphbs = require("express-handlebars");
app.engine("handlebars", exphbs());
app.set("view engine", "handlebars");
// ------------------------- EXPRESS MIDDLEWARE -------------------------
const logger = require("morgan");
const cookieParser = require("cookie-parser");
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
// ------------------------------- MONGODB ------------------------------
const mongoose = require("mongoose");
const MONGODB_URI = process.env.MONGODB_URI;
const connectDB = async () => {
try {
await mongoose.connect(MONGODB_URI, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
console.log("Connected to MongoDB");
} catch (err) {
console.log(err);
throw err;
}
}
connectDB();
// ---------------------------- MOUNT ROUTERs ----------------------------
app.use("/api", require("./routes/api"));
app.use("/", require("./routes/render"));
// --------------------------- ERROR HANDLING ---------------------------
const createError = require("http-errors");
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
// ---------------------------- START SERVER ----------------------------
const PORT = process.env.PORT;
// grab instance of http server for socket.io
const server = app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
// ------------------------------ SOCKET.IO -----------------------------
const io = require("socket.io")(server);
// a socket connects to the server
io.on("connection", (socket) => {
// new post by this socket
socket.on("new post", (data) => {
// emit to all users
socket.broadcast.emit("new post", data);
});
// todo handle disconnect
});
module.exports = app;