-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (58 loc) · 1.88 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
const express = require("express");
const path = require("path");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const passport = require("passport");
const http = require("http");
const chalk = require("chalk");
const compression = require("compression");
const helmet = require("helmet");
const cors = require("cors");
const SocketIO = require("socket.io");
var whitelist = ["http://localhost:3000", "https://twitter-rp.herokuapp.com"];
var corsOptions = {
exposedHeaders: ["x-auth-token"],
origin: function(origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
}
};
//create express application
const app = express();
/**
* MIDDLEWARES
*/
// set up cors to allow us to accept requests from our client
//app.use(cors(corsOptions));
//passport authentication strategy for twitter
// initalize passport
app.use(passport.initialize());
require("./config/passport")(passport);
app.use(morgan("dev"));
// gzip compression
app.use(compression());
app.use(helmet());
//making body available to read in request object
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Redirect to api routes
app.use("/api", require("./routes"));
const server = http.createServer(app);
const io = SocketIO(server);
require("./services/twitterService")(io, app);
// setUserActivityWebhook(app);
// Redirect to client/build to serve html for any router other than /api
if (process.env.NODE_ENV === "production") {
app.use("/", express.static(path.join(__dirname, "./client/build")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const port = process.env.PORT || 5000;
// 🌎 Listen to PORT
server.listen(port, () =>
console.log(chalk.magenta(`server eavesdropping on port ${port}`))
);