-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 1.78 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
// THESE ARE NODE APIs WE WISH TO USE
const express = require('express')
const cors = require('cors')
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser')
const config = require("config");
const path = require('path');
const sockets = require('./sockets');
// CREATE OUR SERVER
const PORT = process.env.PORT || 4000;
const app = express()
// SETUP OUR OWN ROUTERS AS MIDDLEWARE
const piecesRouter = require('./routes/pieces-router')
// For local testing
app.use(express.urlencoded({ extended: true }))
app.use(cors({
origin: ["http://localhost:3000"],
credentials: true
}))
app.use(express.json())
app.use(cookieParser())
app.use('/api', piecesRouter);
app.use('/api/url', (req, res) => {
res.json({ env: process.env.PUBLIC_URL })
})
// For production build
if (process.env.NODE_ENV === "production") {
// SETUP THE MIDDLEWARE
app.use(express.urlencoded({ extended: true }))
app.use(cors({
origin: [process.env.PUBLIC_URL || "https://pieces-316.herokuapp.com/"],
credentials: true
}))
//"http://pieces-316.herokuapp.com",
app.use(express.json())
app.use(cookieParser())
app.use('/api', piecesRouter);
// app.use(express.static("client/build"));
// app.get("*", (req, res) => {
// res.sendFile(path.resolve(__dirname, "/client/build", "index.html"));
// });
}
const serverRouter = require('./routes/server-router');
app.use('/', serverRouter);
// CONNECT TO DATABASE
mongoose.connect(config.get("mongo_uri"), { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
var server = app.listen({ port: PORT }, () => {
console.log(`Server is running on port ${PORT}`)
});
sockets(server);
})
.catch(error => {
console.log(error)
});