-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
64 lines (56 loc) · 1.65 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
61
62
63
64
const express = require("express");
require("express-async-errors");
const app = express();
const cors = require("cors");
const mongoose = require("mongoose");
const helmet = require("helmet");
const flocBlock = require("floc-block");
const logger = require("./utils/logger");
const config = require("./utils/config");
const productsRouter = require("./controllers/products");
const usersRouter = require("./controllers/users");
const loginRouter = require("./controllers/login");
const middleware = require("./utils/middleware");
const path = require("path");
mongoose
.connect(config.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
})
.then(() => {
logger.info("connected to db");
})
.catch(err => {
logger.error("Error connecting to db", err.message);
});
// app.set('trust proxy', 1) // When using rate limiter
app.use(cors());
app.use(helmet());
app.use(flocBlock());
app.use(express.json());
app.use(middleware.tokenExtractor);
app.use(middleware.requestLogger);
if (process.env.NODE_ENV === "production") {
app.use(express.static("build"));
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "build/index.html"), err => {
if (err) {
res.status(500).send(err);
}
});
});
}
app.use("/api/login", loginRouter);
app.use("/api/users", usersRouter);
app.use("/api/products", productsRouter);
// const rateLimit = require("express-rate-limit");
// const apiLimiter = rateLimit({
// windowMs: 15 * 60 * 1000, // 15 minutes
// max: 100,
// });
// app.use("/api/login", apiLimiter);
app.use(middleware.unknownEndpoint);
app.use(middleware.errorHandler);
module.exports = app;