-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (48 loc) · 1.53 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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
require("dotenv").config();
const helmet = require("helmet");
// const rateLimit = require("express-rate-limit");
// set up express
// const limiter = rateLimit({
// windowMs: 15 * 60 * 1000, // 15 minutes
// max: 100 // limit each IP to 100 requests per windowMs
// });
// app.use(limiter);
mongoose.connect(
process.env.MONGODB_CONNECTION_STRING,
{
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
},
(err) => {
if (err) throw err;
console.log("MongoDB connection established");
}
);
const PORT = process.env.PORT || 5000;
const app = express();
app.use(express.json());
app.use(cors());
// app.use(express.static("client/build"))
const root = require('path').join(__dirname, 'client', 'build')
app.use(express.static(root));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
if (req.method === 'OPTIONS') {
res.header("Access-Control-Allow-Methods", "PUT, POST, DELETE, GET");
return res.status(200).json({});
}
next();
});
// set up routes
app.use("/api/products", require("./routes/productRouter"));
app.use("/api/users", require("./routes/userRouter"));
app.get("*", function (req, res) {
res.sendFile('index.html', { root });
})
app.use(helmet());
app.listen(PORT, () => console.log(`The server has started on port: ${PORT}`));