-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (50 loc) · 1.49 KB
/
app.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 cors = require("cors");
const mongoose = require("mongoose");
const app = express();
const PORT = process.env.PORT || 5000;
const rateLimit = require("express-rate-limit");
const { MONGOURI } = require("./config/keys.js");
mongoose.connect(MONGOURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection.on("connected", () => {
console.log("Connected to MongoDB");
});
mongoose.connection.on("error", (err) => {
console.log("Failed to connect due to ", err);
});
require("./models/user");
require("./models/post");
var limiter = new rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 500,
});
app.use(express.json());
app.use(cors());
app.use("*", limiter);
app.use("/api", limiter);
app.use("/api", require("./routes/auth"));
app.use("/api", require("./routes/post"));
app.use("/api", require("./routes/user"));
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
res.setHeader("X-Frame-Options", "SAMEORIGIN");
next();
});
if (process.env.NODE_ENV == "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
app.listen(PORT, (err) => {
if (err) {
console.log(`Error while running app : ${err}`);
} else {
console.log(`Server is running on ${PORT}`);
}
});