forked from youknowme786/Chews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (41 loc) · 1.4 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
const express = require("express");
const path = require("path");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
const PORT = process.env.PORT || 3001;
const app = express();
// Configure body parser for AJAX requests
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
require("./routes/api-routes")(app);
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
} else {
// Use express.static to serve the public folder as a static directory
app.use(express.static("public"));
}
// Set up promises with mongoose
mongoose.Promise = global.Promise;
// Connect to the Mongo DB
mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost/chewsdb", {
useMongoClient: true
});
var http = require("http").Server(app);
var io = require("socket.io")(http);
console.log("pls connect to socket.io")
io.on("connection", socket => {
console.log("user connected");
socket.on("message", message => {
console.log("socket received message from client:", message);
io.emit("message", message);
});
});
// Send every request to the React app
// Define any API routes before this runs
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
http.listen(PORT, function() {
console.log(`🌎 ==> Server and socket now on port ${PORT}!`);
});