generated from Codaisseur/express-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (76 loc) · 2.57 KB
/
index.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
const express = require("express");
const corsMiddleWare = require("cors");
const http = require("http");
const { Server } = require("socket.io");
// Auth middleware: our own code. Checks for the existence of a token in a header called `authentication`.
const authMiddleWare = require("./auth/middleware");
const authRouter = require("./routers/auth");
const moodsRouter = require("./routers/moods");
const activitiesRouter = require("./routers/activities");
const passionsRouter = require("./routers/passions");
const avatarsRouter = require("./routers/avatar");
const { PORT } = require("./config/constants");
// Create an express app
const app = express();
const server = http.createServer(app);
/**
* Middlewares
*
* It is advisable to configure your middleware before configuring the routes
* If you configure routes before the middleware, these routes will not use them
*
*/
// CORS middleware: * Since our api is hosted on a different domain than our client
// we are are doing "Cross Origin Resource Sharing" (cors)
// Cross origin resource sharing is disabled by express by default
app.use(corsMiddleWare());
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`);
socket.on("join_room", (data) => {
socket.join(data);
console.log(`User with ID: ${socket.id} joined room: ${data}`);
});
socket.on("send_message", (data) => {
socket.to(data.room).emit("receive_message", data);
});
socket.on("disconnect", () => {
console.log("User Disconnected", socket.id);
});
});
// express.json():be able to read request bodies of JSON requests a.k.a. body-parser
const bodyParserMiddleWare = express.json();
app.use(bodyParserMiddleWare);
/**
* Routes
*
* Define your routes and attach our routers here (now that middlewares are configured)
*/
app.use("/auth", authRouter);
app.use("/moods", moodsRouter);
app.use("/activities", activitiesRouter);
app.use("/passions", passionsRouter);
app.use("/avatars", avatarsRouter);
// POST endpoint which requires a token for testing purposes, can be removed
app.post("/authorized_post_request", authMiddleWare, (req, res) => {
// accessing user that was added to req by the auth middleware
const user = req.user;
// don't send back the password hash
delete user.dataValues["password"];
res.json({
youPosted: {
...req.body,
},
userFoundWithToken: {
...user.dataValues,
},
});
});
server.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});