-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
65 lines (56 loc) · 1.85 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
const express = require("express");
const morgan = require("morgan");
const helmet = require("helmet");
const { join } = require("path");
const cors = require("cors");
const bodyParser = require("body-parser");
const logger = require("morgan");
const socket = require("socket.io");
const userSeeds = require("./seeders/20200731013520-Users");
const assignmentSeeds = require("./seeders/20200729180756-Assignment");
const users_assignmentsSeeds = require("./seeders/20200805032409-users_assignments");
// Initialize Express
const app = express();
const server = require("http").createServer(app);
const PORT = process.env.PORT || 8080;
const db = require("./models");
//Middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(morgan("dev"));
app.use(helmet());
app.use(express.static(join(__dirname, "build")));
// Routes
// const apiRoutes = require("./routes/API");
// Configure middleware
// Use morgan
app.use(logger("dev"));
// Use body parser
app.use(bodyParser.json());
// Use cors
app.use(cors());
// Static directory
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// Routes
// =============================================================
const routes = require("./routes");
app.use(routes);
const io = socket(server);
io.on("connection", (socket) => {
console.log(socket.id);
socket.on("SEND_MESSAGE", function(data){
io.emit("RECEIVE_MESSAGE", data);
})
});
// Syncing our sequelize models and then starting our Express app
// =============================================================
db.sequelize.sync({ force: false }).then(function () {
server.listen(PORT, function () {
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`);
userSeeds.up(db.User);
assignmentSeeds.up(db.Assignment);
users_assignmentsSeeds.up(db.Users_Assignments);
});
});