-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
60 lines (49 loc) · 1.57 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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const path = require("path");
const ws = require("ws");
const PORT = process.env.PORT || 5000; //5000 for web
const app = express();
app.set("port", process.env.PORT || 5000); //5000 for web
app.use(cors());
app.use(bodyParser.json());
const MongoClient = require("mongodb").MongoClient;
require("dotenv").config();
const url = process.env.MONGODB_URI;
const client = new MongoClient(url);
client.connect();
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
accessKeyId: process.env.BUCKETEER_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.BUCKETEER_AWS_SECRET_ACCESS_KEY,
region: 'us-east-1',
});
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, DELETE, OPTIONS"
);
next();
});
const server = app.listen(PORT, () => {
console.log("Server listening on port " + PORT);
});
const wss = new ws.Server({ server: server });
var api = require("./api.js");
api.setApp(app, wss, client);
// For Heroku deployment
// Server static assets if in production
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("frontend/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "frontend", "build", "index.html"));
});
}
module.exports = { app, client, server, wss, s3 };