-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
68 lines (55 loc) · 1.66 KB
/
server.ts
File metadata and controls
68 lines (55 loc) · 1.66 KB
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
import "./config/env";
import express from "express";
import helmet from "helmet";
import cors from "cors";
import mongoose from "mongoose";
import path from "path";
import { rateLimit } from "express-rate-limit";
import auth from "./routes/auth";
import { getMongoURI } from "./config/env";
import { FIVE_MINUTES_MS, ONE_HOUR_MS } from "./const/times";
const app = express();
app.use(helmet());
app.use(
cors({
origin:
process.env.NODE_ENV === "production"
? process.env.ALLOWED_ORIGINS?.split(",")
.map((o) => o.trim())
.filter(Boolean) || false
: true,
credentials: true,
}),
);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const globalLimiter = rateLimit({
windowMs: FIVE_MINUTES_MS,
limit: 200,
message: "Too many requests. Please try again later.",
});
app.use(globalLimiter);
mongoose.connect(getMongoURI());
// const clientBuildPath = path.join(__dirname, "client", "build");
// app.use(express.static(clientBuildPath));
const loginLimiter = rateLimit({
windowMs: FIVE_MINUTES_MS,
limit: 3,
message: "Maximum login attempts exceeded. Please try again later.",
});
const signupLimiter = rateLimit({
windowMs: ONE_HOUR_MS,
limit: 3,
message: "Maximum accounts created. Please try again later.",
});
app.use("/auth/login", loginLimiter);
app.use("/auth/signup", signupLimiter);
app.use("/auth", auth);
// app.get("/{*splat}", (_req, res) => {
// res.sendFile(path.join(clientBuildPath, "index.html"));
// });
const port = Number(process.env.PORT) || 3001;
const server = app.listen(port, () => {
console.log(`Server listening on port: ${port}`);
});
export default server;